Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- random.walk <- function(n){
- steps <- rbinom(n, 1,0.5)*2-1
- pos <-0
- k<-n
- pos.hx <- seq(from=0, to=0, length.out=n)
- for (i in 1:n){
- pos <- steps[i]+pos
- pos.hx[i] <- pos
- if (pos==0){k<-i
- return(k)
- }
- }
- return(k)
- }
- simulations <- function(n, lim=100000){
- termination <- seq(from=0, to=0, length.out=n)
- for (i in 1:n){
- termination[i]<-random.walk(lim)
- }
- no.return <- sum(termination==lim)
- avg.walk<-mean(termination[termination<lim])
- return(c(no.return, avg.walk))
- }
- # Two input params: number of simulations, and max number of coin flips
- # two output values: total number of simulations that did not return to origin,
- # and avg number of flips/steps taken to return
- simulations(10000, lim=100000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement