# Mathematics in action: returning from a random walk

Three 2-dimensional random walks. All three start at the black circle and finish, after 100 steps, at a coloured square. Later steps are in darker colours. Considerable backtracking occurs.

We have discussed one-dimensional random walks, but it is possible to have random walks in more than one dimension. In two dimensions (above), we can go left, right, forward, and back. A random walk in two dimensions can be played as a kind of game (as can one-dimensional random walks). In three dimensions (below) we can also move vertically. Three-dimensional random walks are related to the motion of molecules in a gas or liquid.

Three 3-dimensional random walks. All three start at the black circle (in the centre of the cube) and finish, after 100 steps, at a coloured square. Later steps are in darker colours.

One very interesting question is whether a random walk ever returns to its starting point. In one dimension, the probability of returning in exactly n ≥ 1 steps is 0 if n is odd, and C(nn/2) / 2n if n is even, where C(nk) is the number of ways of choosing k items out of n, which is defined by C(nk) = n! / (k! (nk)!).

For large numbers n, Stirling’s approximation says that n! is approximately sqrt(2πn)(n/e)n. If we let m = n/2, some tedious algebra gives the probability of returning in exactly n = 2m steps as 1/sqrt(πm) ≈ 0.56/sqrt(m). When I ran some experiments I actually got a factor of 0.55, which is pretty close. Given infinite time, the expected number of times we return to the starting point is then:

0.56 (1 + 1/sqrt(2) + 1/sqrt(3) + 1/sqrt(4) + …) = ∞

This means that an eventual return to the starting point is certain. It may take a while, however – in 100 random walks, summarised in the histogram below, I once had to wait for 11452 steps for a return to the starting point.

Random walks in two dimensions can be understood as two random walks in one dimension happening simultaneously. We return in exactly n = 2m steps if both one-dimensional walks return together. The probability is therefore the one above squared, i.e. 1/(πm) ≈ 0.318/m. Again, given infinite time, the expected number of times we return to the starting point is:

0.318 (1 + 1/2 + 1/3 + 1/4 + …) = ∞

This means that a return to the starting point is also theoretically certain, although it will take much, much longer than for the one-dimensional case. In a simple experiment, four random walks returned to the starting point in 6814, 2, 21876, and 38 steps respectively, but the fifth attempt took so long that I gave up. In three or more dimensions, a return to the starting point might never occur.

# Mathematics in action: Risky Random Walks

A game of Risk (photo: A.R.N. Rødner)

The board game Risk, though far from being my favourite game (and rated only 5.59/10 on Board Game Geek), nevertheless has some interesting strategic aspects and some interesting mathematical ones.

Combat units in Risk (photo: “Tambako The Jaguar”)

A key feature of the game is a combat between a group of N attacking units and a group of M defending units. The combat involves several steps, in each of which the attacker rolls 3 dice (or N if N < 3) and the defender rolls 2 dice (or 1 if M = 1). The highest value rolled by the attacker is compared against the highest rolled by the defender, and ditto for the second highest values, as shown in the picture below. For each comparison, if the attacker has a higher value, the defender loses a unit, while if the values are tied, or the defender has a higher value, the attacker loses a unit.

Comparing attacker (left) and defender (right) dice in Risk (photo: “Val42”)

Working through the 65 possibilities, the attacker will be down 2 units 29.3% of the time, both sides will have equal losses 33.6% of the time, and the attacker will be up 2 units (relative to the defender) 37.2% of the time. On average, the attacker will be up very slightly (0.1582 of a unit). A fairly simple computation (square each of the outcome-mean differences −2.1582, −0.1582, and 1.8418; multiply by the corresponding probabilities 0.293, 0.336, and 0.372 and sum; then take the square root) shows that the standard deviation of the outcomes is 1.6223.

When this basic combat step is repeated multiple times, the result is a random walk. For example, with 10 steps, the mean attacker advantage is 1.582 units, and (by the standard formula for random walks discussed in a previous post) the standard deviation is 1.6223 times the square root of the number of steps, i.e. 5.1302.

The histogram below shows the probability of the various outcomes after 10 steps, ranging from the attacker being 20 units down (0.0005% of the time) to the attacker being 20 units up (0.005% of the time). Superimposed on the plot are a bell curve with the appropriate mean and standard deviation, together with five actual ten-step random walks. While the outcome does indeed favour the attacker, there is considerable random variability here – which makes the game rather unpredictable.

# Mathematics in action: Flipping coins

Our second post about probability is about flipping coins and random walks. Once again, I’ve used random numbers from www.random.org, but I’ve represented the coin flips as 1 for heads and −1 for tails. The expected mean is therefore 0 (I actually got 0.0034), and the expected variance is s − 02 = 1, where s = 1 is the mean of the squares of the numbers −1 and 1 (for the variance, I actually got 1.000088).

We then consider a random walk where we repeatedly flip a coin and walk a block west if it’s tails and a block east if it’s heads. In particular, we consider doing so 144 times. How far would we expect to get that way? Well, on average, nowhere – we are adding 144 coin flips, and the mean distance travelled will be 0. The coloured lines in the diagram above show ten example random walks (with time running vertically upwards). These finish up between 18 blocks west and 20 blocks east of the starting point, so the mean of 0 represents an average of outcomes where we wind up several blocks west or east.

Since we can add variances, the variance for the random walk will be the variance of a single step times the number of steps. Alternatively, the standard deviation will be the standard deviation of a single step times the square root of the number of steps. In this case, the expected standard deviation of the random walk is 12 (for the ten random walks in the diagram above, I actually got 14.55; for a larger sample, 12.086). The width of the bell curve in the diagram illustrates the theoretical standard deviation (the height of the bell curve is not meaningful).

The expected absolute value of the distance travelled depends on the mean value of half a bell curve: it is 12 × sqrt(2/π) = 9.5746 (I actually got 12.6; for a larger sample, 9.631). So, for our random walk, we can expect to wind up around 10 blocks from the starting point – sometimes more, sometimes less. Naturally, this is just a simple example – there’s a lot more interesting mathematics in the theory of random walks, especially when we consider travelling in more than one dimension.