Random Walk from the Centre of a Unit Square and Cube
✠
Riddler Express
You start at the centre of the unit square and pick a random direction to move in, with all directions equally likely. You move along this direction until you reach a point on the perimeter of the square. On average, how far can you expect to have travelled?
Solution
Place the square with corners at (0,0), (1,0), (1,1), (0,1). By symmetry, the mean distance over all directions equals the mean over the triangle with vertices (1/2,1/2), (1,1/2), (1,1). Pick a direction θ∈[0,π/4]; the distance to the right edge is d=1/(2cosθ).
Weighting by the uniform density on the fundamental sector, E[d]=∫0π/42cosθ1⋅π4dθ=π2∫0π/4secθdθ. The antiderivative of secθ is ln∣secθ+tanθ∣, so E[d]=π2ln(2+1)=πln(3+22)≈0.5611.
Why the ∫secxdx step works: four derivations
Method 1: multiplication by a clever form of 1.
Multiply by (secx+tanx)/(secx+tanx)=1: ∫secxdx=∫secx+tanxsec2x+secxtanxdx. With u=secx+tanx, the numerator equals du, so the integral equals ln∣u∣+C=ln∣secx+tanx∣+C.
Method 2: Weierstrass substitution.
With t=tan(x/2), the half-angle formulas give sinx=2t/(1+t2), cosx=(1−t2)/(1+t2), and dx=2dt/(1+t2). Then ∫secxdx=∫1−t22dt=ln1−t1+t+C=lntan(4π+2x)+C.
Method 3: complex exponentials.
Since cosx=(eix+e−ix)/2, ∫secxdx=∫e2ix+12eixdx. Substitute u=eix, giving ∫2/(i(u2+1))du=−2iarctan(eix)+C, which on the real line reduces to ln∣secx+tanx∣+C.
Method 4: via hyperbolic functions.
Setting x=it, cos(it)=cosht, so ∫secxdx=i∫sechtdt=2iarctan(tanh(t/2))+C, which again equals ln∣secx+tanx∣+C after substituting t=−ix.
Now, you start at the centre of the unit cube. Again, you pick a random direction to move in, with all directions being equally likely. You move along this direction until you reach a point on the surface of the cube. On average, how far can you expect to have travelled?
Solution
Place the cube with corners at (0,0,0) and (1,1,1). A random direction on the sphere is written as v=(sinφcosθ,sinφsinθ,cosφ) with θ∈[0,2π) and φ∈[0,π]; the uniform measure on the sphere is (4π)−1sinφdφdθ. From the centre, the distance along v to the boundary is d=2m1,m=max(∣sinφcosθ∣,∣sinφsinθ∣,∣cosφ∣).
The cube’s direction sphere is cut into congruent pieces by its symmetries: 8 octants from the sign choices, and within each octant 3 regions according to which coordinate is largest in magnitude. Restrict to the region in the first octant where the x-component is largest (the y and z order is left free), which is therefore 241 of the sphere: θ∈[0,4π],φ∈[arctan(secθ),2π]. In this region, d=1/(2sinφcosθ), and the sinφ in the measure cancels the sinφ in the denominator of d. Multiplying by 24 and dividing by 4π: E[d]=π3∫0π/4cosθ2π−arctan(secθ)dθ=π3∫0π/4cosθarctan(cosθ)dθ, using arctanx+arctan(1/x)=π/2 for x>0.
The integral does not collapse to elementary constants. Numerically, ∫0π/4cosθarctan(cosθ)dθ≈0.63951, giving E[d]≈0.6106.
Distance to each face
From the centre (1/2,1/2,1/2), the distance to each face in direction (sinφcosθ,sinφsinθ,cosφ) is:
right (x=1): t=1/(2sinφcosθ) if sinφcosθ>0
left (x=0): t=1/(2∣sinφcosθ∣) if sinφcosθ<0
front (y=1): t=1/(2sinφsinθ) if sinφsinθ>0
back (y=0): t=1/(2∣sinφsinθ∣) if sinφsinθ<0
top (z=1): t=1/(2cosφ) if cosφ>0
bottom (z=0): t=1/(2∣cosφ∣) if cosφ<0
The actual distance travelled is the minimum of these six values.
Monte Carlo verification
import numpy as np
def simulate_random_walk_3d(num_simulations=1_000_000):
"""Simulate random walks from the centre of the unit cube."""
x0, y0, z0 = 0.5, 0.5, 0.5
theta = np.random.uniform(0, 2 * np.pi, num_simulations)
u = np.random.uniform(-1, 1, num_simulations) # cos(phi), uniform
s = np.sqrt(1 - u * u) # sin(phi)
dx, dy, dz = s * np.cos(theta), s * np.sin(theta), u
t_right = np.where(dx > 0, (1 - x0) / dx, np.inf)
t_left = np.where(dx < 0, -x0 / dx, np.inf)
t_front = np.where(dy > 0, (1 - y0) / dy, np.inf)
t_back = np.where(dy < 0, -y0 / dy, np.inf)
t_top = np.where(dz > 0, (1 - z0) / dz, np.inf)
t_bottom = np.where(dz < 0, -z0 / dz, np.inf)
return np.minimum.reduce([t_right, t_left, t_front,
t_back, t_top, t_bottom])
np.random.seed(42)
d = simulate_random_walk_3d(10**6)
print(f"simulated: {d.mean():.6f}")
# simulated: 0.610596
Convergence
Running Monte Carlo for varying sample sizes:
Sample size
Mean
Std. error
103
0.6133
0.0053
104
0.6109
0.0017
105
0.6105
0.00053
106
0.6106
0.00017
107
0.61060
0.00005
Integral
0.6106
—
Comparison of the two answers
Dimension
Exact form
Numerical
2D (square)
πln(3+22)
0.5611
3D (cube)
π3∫0π/4cosθarctan(cosθ)dθ
0.6106
The expected distance grows only modestly (by about 9%) from 2D to 3D. Although an extra dimension opens longer “body-diagonal” directions (up to 3/2≈0.866), the cube’s six faces cut most rays off quickly, so the uniform-direction average is only a little above the inradius 0.5.