Hi
I try to do a simultation for Gambler's Ruin. I wrote this -
r <- 20 #Number of turns
p <- 0.5 # Probability of moving forward
y <- c() # Progress Reporting
x0 <- 0 #initial step
A_new_step <- c()
B_new_step <- c()
a <- -10
b <- 10
random_walk<-function(x0, a, b, p , r){
for(i in 1:r){
y<-0
x<-sample(c(-1,1),size = r , replace = TRUE)
y[i+1]<-y[i]+x
if(y[i] == a ){
A_new_step<-(y[i:(r)] = a)
break
return(A_new_step)
}
if(y[i] == b ){
B_new_step<-(y[i:r] =b )
break
return(B_new_step)
}
}
return(y)
}