Displaying 2 results from an estimated 2 matches for "ranwalk2".
Did you mean:
ranwalk
2007 Oct 24
2
random walk w/ reflecting boundary: avoid control construct?
Dear expeRts,
recently I asked for a nice way to re-program a problem
without using control constructs such as "for" or
"sapply(1:length(x), ...". Is there a way to program
a random walk with a reflecting boundary without resorting
to such constructs? A working solution is
ranwalk <- function(length, bound) {
k <- cumsum(sample(c(-1, 1), length, replace=TRUE))
2007 Oct 24
0
random walk w/ reflecting boundary: avoid control construct? [SEC=UNCLASSIFIED]
...; k
> }
>
> but it uses "while" and has the same expression in the "while"
> statement and the following line. Is there a sensible way to reprogram
> it using the "whole object"
> approach?
Does this satisfy your 'sensible' sensibility?
ranwalk2 <-
function(length., bound) {
k <- cumsum(sample(c(-1, 1), length., replace=TRUE))
lk <- rep( c( 0:bound, (bound-1):(-bound), (1-bound):(-1)),
length=max(abs(k))+1)
sign(k)*lk[ 1 + abs(k) ]
}
HTH,
Chuck
>
> _____________________________________________...