Skip to content
Vamshi Jandhyala

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)

From the centre, a ray at angle θ\theta to the nearest wall travels 12secθ\tfrac12\sec\theta before meeting that wall.

Solution

Put the centre at the origin, so the four walls are x=±12x=\pm\tfrac12 and y=±12y=\pm\tfrac12. A ray in direction θ\theta first meets the wall that is perpendicular-closest, and the distance to that wall is r(θ)=12max ⁣(cosθ,sinθ).r(\theta)=\frac{1}{2\max\!\bigl(|\cos\theta|,|\sin\theta|\bigr)}. By the eightfold symmetry of the square it is enough to average over θ[0,π4]\theta\in[0,\tfrac\pi4], where the right wall x=12x=\tfrac12 is hit at distance 12secθ\tfrac12\sec\theta. Hence E[r]=12π02πr(θ)dθ=2π0π/4 ⁣secθdθ=2πln ⁣(2+1)0.5611.\begin{aligned} E[r]&=\frac{1}{2\pi}\int_0^{2\pi} r(\theta)\,d\theta =\frac{2}{\pi}\int_0^{\pi/4}\!\sec\theta\,d\theta\\ &=\frac{2}{\pi}\,\ln\!\bigl(\sqrt2+1\bigr)\approx\boxed{0.5611}. \end{aligned}

The computation

Fire random rays from the centre: for each direction take the distance to the nearest of the four walls, 12/max(cosθ,sinθ)\tfrac12/\max(|\cos\theta|,|\sin\theta|), and average. The Monte Carlo matches the closed form 2πln(2+1)\tfrac2\pi\ln(\sqrt2+1).

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 124\tfrac{1}{24} of directions whose nearest face is the top and whose azimuth θ\theta lies in [0,π4][0,\tfrac\pi4]. With φ\varphi the polar angle, the travel distance is 1/(2cosθsinφ)1/(2\cos\theta\sin\varphi) and the top face is nearest once φarctan(secθ)\varphi\ge\arctan(\sec\theta). Weighting by the solid-angle density 14πsinφ\tfrac1{4\pi}\sin\varphi and the 2424-fold symmetry, the sinφ\sin\varphi cancels and E[r]=3π0π/4secθ(π2arctan(secθ))dθ0.6107.E[r]=\frac{3}{\pi}\int_0^{\pi/4}\sec\theta\Bigl(\frac\pi2-\arctan(\sec\theta)\Bigr)\,d\theta \approx\boxed{0.6107}. (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, 12/max(vx,vy,vz)\tfrac12/\max(|v_x|,|v_y|,|v_z|). 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