inst/samples/rw.R

rw <- function(n = 100) {
    xpos <- ypos <- numeric(n)
    truefalse <- c(TRUE, FALSE)
    plusminus1 <- c(1, -1)
    for(i in 2:n) {
        ## Decide whether we are moving horizontally
        ## or vertically.
        if (sample(truefalse, 1)) {
            xpos[i] <- xpos[i-1] + sample(plusminus1, 1)
            ypos[i] <- ypos[i-1]
        }
        else {
            xpos[i] <- xpos[i-1]
            ypos[i] <- ypos[i-1] + sample(plusminus1, 1)
        }
    }
    list(x = xpos, y = ypos)
}

Try the proftools package in your browser

Any scripts or data that you put into this service are public.

proftools documentation built on July 8, 2020, 5:20 p.m.