Random walk

Below is given one-dimensional random walk simulation. Particles start from \(0\) and in every step each particle change its position by \(+1\) with probability \(p\) or by \(-1\) with probability \(1-p\).

Number of experiments (particles) (1-10.000)
Number of steps in each experiment (10-10.000)
Probability \(p\) (in percents) (0-100)
Start simulation

Particles trajectories:

Your browser doesn't support canvas.

Distribution of final positions:

Your browser doesn't support canvas.


\(\mathrm E \left(S_n\right)\) \(\sigma \left(S_n\right)\) \(\min S_n\) \(\max S_n\)
Model (see below)

Mathematical model

Let \(Z_i\) be random variable that equals to change of a particle position in step \(i\). Variable \(Z_i\) has probability distribution \[\begin{pmatrix} -1 & 1 \\ 1-p & p\end{pmatrix}.\]

If the particle starts from the \(0\), then it's position after \(n\) steps is given by the random variable \(S_n = Z_1 + Z_2 + \dots + Z_n\). Therefore, expectation of the \(S_n\) is given by \[\mathrm E \left(S_n\right) = \sum_{i=1}^n \mathrm E \left(Z_i\right) = n\left(2p-1\right).\] Specially, if \(p=0.5\) then \(\mathrm E \left(S_n\right) = 0.\)

For \(1\le i\lt j \le n\), random variables \(Z_i\) and \(Z_j\) are independent, so we have \(\mathrm E \left(Z_i Z_j\right) = \mathrm E \left(Z_i\right) \mathrm E \left(Z_j\right).\) We can now easily calculate variance of \(S_n:\) \[\begin{aligned}\mathrm{Var}\left(S_n\right) &= \mathrm E \left(S^2_n\right) - \mathrm E ^2\left(S_n\right) \\ &= \mathrm E \left(\sum_{i=1}^n Z_i^2 + \sum_{1\le i\ne j\le n} Z_i Z_j \right) - \left(n\left(2p-1\right)\right)^2 \\ &= \sum_{i=1}^n \mathrm E \left(Z_i^2\right) + \sum_{1\le i\ne j\le n} \mathrm E \left(Z_i\right) \mathrm E \left(Z_j\right) - n^2\left(2p-1\right)^2 \\ &= n+2n\left(n-1\right)\left(2p-1\right)^2 - n^2\left(2p-1\right)^2 \\ &= 4np(1-p).\end{aligned}\] Specially, if \(p=0.5\) then \(\mathrm{Var}\left(S_n\right) = n.\)

Source code available on Github.