Here's some imperative scala:
object particle {
def main(args:Array[String]) = {
val simulations = args(0).toInt
val rng = new util.Random
val reachedZero = (1 to simulations).map(_=> {
var t = 0
var z = 1+rng.nextInt(1000)
do {
z += {if (rng.nextBoolean) 1 else -1}
t +=1
} while( z != 0 && t<1000)
z==0
})
println( reachedZero.count(_==true)*1.0d/simulations )
}
}
---
>scala particle 1000000
>0.024782
Some intuition: If there were only 2 positions {1,2}, and you picked 1 with probability 0.5, you could get to 0 by going down once with probability 0.5. Going up wouldn't help since you'd have to go down to be back at 1, at which point you've exhausted your chances. Suppose you picked 2 with probability 0.5, you'd have to go down twice consecutively to hit 0, which has a probability of 0.5^2. So overall, your probability is 0.5^2 + 0.5^3 = 0.375
You can try working it out with 3 positions & 3 turns, and suddenly its not so easy....:)So my question is, how are you dealing with the initial condition?
Suppose there were [1,k] spots. You are assigned an intial value from [1,k] and you then have to reach 0 before time t=k. Here's my code to solve the general case
object particle {
def main(args:Array[String]) = {
val simulations = args(0).toInt
val rng = new util.Random
(2 to 1000).foreach(spots=>{
val reachedZero = (1 to simulations).map(_=> {
var t = 0
var z = 1+rng.nextInt(spots)
do {
z += {if (rng.nextBoolean) 1 else -1}
t +=1
} while( z != 0 && t<spots)
z==0
})
val prob = reachedZero.count(_==true)*1.0d/simulations
printf("%4d spots: %.4f\n",spots,prob )
})
}
}
>scala particle 1000000
2 0.3773
3 0.329
4 0.292
5 0.2714
6 0.2624
7 0.2428
8 0.2247
9 0.222
10 0.2157
....
So your chances drop from 37% to 21% as the interval expands to 10 spots. At 100 spots, its 8%. By 1000 spots, you have a meager 2.5% chance of crossing 0 before 1000 seconds.The answer will certainly be a function of n (because, for example, I could give you n = 1001 and that should have 0 probability whereas if I were to give you n = 1, that would have a very high probability).
My first thought was to write out a recurrence relation f of n and k (where n is the current level and k is the number of moves left). From this, we get: f(n, k) = .5f(n-1, k-1) + .5f(n+1, k-1) with f(n, k) = 0 when n > k and f(0, k) = 1. So to solve this, we just take f(n, 1000) = [a rapidly exploding function].
Any thoughts?
It appears that a basic knowledge of stochastic processes is required to actually be able to solve this problem, so it's one of those things that are actually much more complicated than they first appear.
The keyword is "random walk hitting time".
> Consider the integers from [0,1000]. Suppose a particle starts at position n. At discrete instants of time t=0,1,2,…, the particle moves up or down with p=0.5. What is the probability that the particle reaches 0 before t=1000?
The answer is 50%, right? I can't explain it properly, but my thinking is that the random walk is symmetric and 0 and 1000 should have equal weights. Maybe a good explanation is harder than the correct answer in this case.