Books · The Fiddler: Solutions
Chapter 33
Can You Reach the Edge of the Square?
You start at the centre of a unit square and pick a random direction to move in, with all directions equally likely. On average, how far do you travel before you reach the perimeter?
The Fiddler, Zach Wissner-Gross, October 17, 2025(original post)
Solution
Put the centre at the origin, so the four walls are and . A ray in direction first meets the wall that is perpendicular-closest, and the distance to that wall is By the eightfold symmetry of the square it is enough to average over , where the right wall is hit at distance . Hence
The computation
Fire random rays from the centre: for each direction take the distance to the nearest of the four walls, , and average. The Monte Carlo matches the closed form .
import numpy as np
from math import pi, log, sqrt
rng = np.random.default_rng(0); th = rng.uniform(0, 2*pi, 4_000_000)
d = 0.5 / np.maximum(np.abs(np.cos(th)), np.abs(np.sin(th)))
print(round(d.mean(), 5), round(2/pi*log(1 + sqrt(2)), 5)) # 0.56102 0.5611
Extra Credit
Now you start at the centre of a unit cube and pick a uniformly random direction in three dimensions. On average, how far do you travel before reaching the surface?
Solution
By the cube’s symmetry, average over the of directions whose nearest face is the top and whose azimuth lies in . With the polar angle, the travel distance is and the top face is nearest once . Weighting by the solid-angle density and the -fold symmetry, the cancels and (The source’s value is paywalled; this closed form is my own.)
The computation
The same experiment in 3D: draw directions uniformly on the sphere (normalised Gaussians) and take the distance to the nearest cube face, . The Monte Carlo confirms the integral.
from scipy import integrate
val, _ = integrate.quad(lambda t: (1/np.cos(t))*(pi/2 - np.arctan(1/np.cos(t))), 0, pi/4)
rng = np.random.default_rng(0); v = rng.standard_normal((4_000_000, 3))
v /= np.linalg.norm(v, axis=1, keepdims=True)
print(round((0.5/np.max(np.abs(v), axis=1)).mean(), 5), round(3/pi*val, 5)) # 0.61068 0.61069