Displaying 2 results from an estimated 2 matches for "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))
while( any(abs(k) > bound) ) {
ri <- min(which(abs(k) > bound))
k[ri:length] <- k[ri:length] - 2 * sign(k[ri])
}
k
}
but it uses "while" and has the same exp...
2007 Oct 24
0
random walk w/ reflecting boundary: avoid control construct? [SEC=UNCLASSIFIED]
...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))
> while( any(abs(k) > bound) ) {
> ri <- min(which(abs(k) > bound))
> k[ri:length] <- k[ri:length] - 2 * sign(k[ri])
> }
> k
> }
>
> but it uses...