Mean distance
Root mean square
Variance of x
Back at the start

More information about random walks

Random walk

Send a crowd of walkers off from the same spot and let every one of them take random steps: left, right, up or down. On average they go nowhere, because every direction is equally likely, and yet the crowd spreads out further with every step. Simulate up to 5,000 walks of up to 2,000 steps, watch the paths wander and the cloud of walkers grow, and compare what you see with theory: the mean squared distance grows exactly like the number of steps, the distances follow a Rayleigh curve, and most walkers find their way back to the start. Every walker starts in the middle and takes one step at a time in a random direction: left, right, up or down. Choose how many walkers set off and how many steps each takes, and press Play to watch them spread out. The chart shows how far they ended up from the start, with the curve that theory predicts, or how the mean squared distance grows with every step. Skip jumps to the end and New run lets a fresh crowd of walkers go.

How it works

  1. Choose how many walkers set off and how many steps each takes, and how a step goes: left, right, up or down, to one of 8 neighbours, in any direction, or along a line.
  2. Press Play to watch every walker step at the same time; Skip jumps to the last step and New run sends new walkers.
  3. Read the chart: the distances from the start against the Rayleigh curve, or the mean squared distance against the number of steps.
  4. Compare the tiles with theory: the mean distance, the root mean square √N, the variance of x and the share of walkers back at the start.

Nowhere on average, further all the time

Every step is as likely to go one way as the other, so the expected position after any number of steps is the start itself, and the average of a thousand end points lands within a step or two of the centre. But the variance of the position grows by the same amount with every step: on a grid Var(x) = N/2 after N steps. The mean squared distance from the start is therefore exactly N, and the typical walker is about √N steps away. The dashed circle on the walks has that radius.

The Rayleigh curve

After many steps x and y are each nearly normal, by the central limit theorem, so the distance √(x² + y²) follows a Rayleigh distribution: zero at the start, a peak at √(N/2) and a long tail, with mean 0.886√N. The histogram of the distances shows it forming as the walkers spread; walk along a line instead and the end positions make a normal bell curve with variance N. Turn Theory off to look at the simulation on its own.

Coming home

George Pólya proved in 1921 that a walker on a line or a flat grid returns to its start with certainty, given enough time; in three dimensions it may wander off for good. The Back at the start tile counts the walkers that have stood on the start again at least once. On a line almost all of them have after a few hundred steps; on a grid it creeps up slowly, about 64% after 500 steps and 70% after 2,000, because the return can take a very long time.

Frequently asked questions

What is a random walk?

A path made of random steps. In the simplest version a walker on a grid steps left, right, up or down, each with chance 1/4, independently of the steps before. It is also called the drunkard's walk, after a question Karl Pearson asked in Nature in 1905.

How far from the start does a walker end up?

Typically about √N steps after N steps. The mean squared distance is exactly N for steps of length 1, so the root mean square distance is √N; the mean distance on a flat grid is about 0.886√N. After 500 steps that is about 22 and 20 steps, which the tiles compare with the simulation.

Why is the mean squared distance a straight line?

Because the variances of independent steps add up. Each step adds the same amount, 1 for a unit step, to the expected squared distance, so after N steps it is N. The 8-neighbour walk adds 1.5 per step, because its diagonal steps are √2 long.

Why does the histogram of distances look like that?

After many steps the x and y positions are each close to a normal distribution, and the distance from the start then follows a Rayleigh distribution: no walkers exactly at the start, a peak near √(N/2) and a long tail. On a line the positions form a normal bell curve instead.

Do the walkers come back to the start?

On a line and on a flat grid every walker comes back sooner or later, with probability 1, as George Pólya proved in 1921, but some take a very long time. In three dimensions a walker returns with a chance of only about 34%. The Back at the start tile counts how many have returned so far.

Can I use the picture?

Yes. Save gives a PNG, and the pencil opens it in the PictorX photo editor with the grid, the expected distance, the paths, the walkers, the chart and the labels on layers of their own. Everything is simulated in your browser; nothing is sent to a server.

Random Walk: how do I record a video?

Press the red Record button in the playback bar: it films what the stage shows. Press it again to stop, and the video is saved to your device. Recording is free; you only need to sign in. Without a subscription the video carries a small PictorX logo in a corner. The button beside Record opens the video options: the shape (as shown, square, story 9:16 or wide 16:9), the length (until you stop, or 5, 10, 15 or 30 seconds for social media) and the file: MP4, WebM or an animated GIF. You can add one of three music tracks: calm, bright or beat.

Ready to try it?

Open PictorX in your browser — no install, no credit card required to start.

Go to Editor