A random walker in a 1-dimensional lattice bounded on [0, 20] can move one step to the right with probability 0.7 or one step to the left with probability 0.3. Assuming that he starts at 0, what is the probability that he will reach 20 before returning to zero?
I am trying to simulate this in R but haven't had any success so far. I'm new to R and stochastic processes so I'm not sure how to go about estimating the probability. Here is my code (my apologies for the clumsiness):
walk <- function()
{
games = 10000
p.fwd = 0.7
win = 0
position=0 #tracks the walker's position on the line
total.steps=0 #counts total steps taken to reach home
for(i in 1:games)
{
step = sample(c(-1, 1), size = 1, prob = c(1-p.fwd, p.fwd))
position = position + step
total.steps = total.steps + 1
while(0<position && position<20)
{
step = sample(c(-1, 1), size = 1, prob = c(1-p.fwd, p.fwd))
position = position + step
total.steps = total.steps + 1
}
if(position==20)
{
win = win + 1
}
}
return(win/games)
}
walk()