# simulation code simX <- function(){ u = runif(1) if(u <= 0.5){ return(-1 + sqrt(2*u)) } #else return(1 - sqrt(2 - 2*u)) } xvals = replicate(10000, simX()) hist(xvals, freq=FALSE) # simulate Brownian motion pos <- c(0) for(i in 1:100){ pos <- append(pos, pos[length(pos)] + simX()) } plot(pos) lines(pos) # how much time until distance 10 from origin? timeToTen <- function(){ pos = 0 time = 0 while(abs(pos) < 10){ pos = pos + simX() time = time + 1 } return(time) } times = replicate(1000, timeToTen()) mean(times)