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?
Problem 42.2
You start at the centre of the unit cube. You pick a random direction uniformly on the sphere and move in that direction until reaching the surface of the cube. On average, how far can you expect to have travelled?
Solution, part (a): the square. Place the square with corners at (0,0), (1,0), (1,1), (0,1). By symmetry the mean length 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 ray hits the right edge at distance d(θ)=2cosθ1. Weighting by the fraction of angles in the fundamental sector, 4/π per radian of θ, E[d]=π4∫0π/42cosθ1dθ=π2∫0π/4secθdθ=π2ln(secθ+tanθ)0π/4. At θ=π/4 the bracketed quantity is 2+1; at θ=0 it is 1. Therefore E[dsquare]=π2ln(2+1)=πln(3+22)≈0.5611.■
The integral of secθ admits four classical derivations worth recording.
Method 1. Multiply the integrand by (secx+tanx)/(secx+tanx)=1. The numerator becomes sec2x+secxtanx, which is exactly the derivative of secx+tanx. Setting u=secx+tanx reduces the integral to ∫du/u=ln∣u∣+C=ln∣secx+tanx∣+C.
Method 2 (Weierstrass substitution). With t=tan(x/2), the half-angle formulas give cosx=(1−t2)/(1+t2) and dx=2dt/(1+t2), so ∫secxdx=∫1−t22dt=ln1−t1+t+C=lntan(4π+2x)+C, the last step by the tangent addition formula.
Method 3 (complex exponentials). With cosx=(eix+e−ix)/2 and the substitution u=eix, ∫secxdx=i2∫u2+1du=−2iarctan(eix)+C, which, on the real line, reduces to ln∣secx+tanx∣+C.
Method 4 (hyperbolic). With x=it, cos(it)=cosht, so ∫secxdx=i∫sechtdt=2iarctan(tanh(t/2))+C, again equivalent to ln∣secx+tanx∣+C after substituting back.
Solution, part (b): the cube. Place the cube with corners at (0,0,0) and (1,1,1). A random direction on the sphere is v=(sinφcosθ,sinφsinθ,cosφ), with uniform measure (4π)−1sinφdφdθ. From the centre the distance to the boundary is d=2m1,m=max(∣sinφcosθ∣,∣sinφsinθ∣,∣cosφ∣).
The magnitude m=max(∣x∣,∣y∣,∣z∣) is unchanged by the 48 symmetries of the cube, so it suffices to average over a single fundamental region: the directions in the first octant whose x-component is largest in magnitude. There are 24 congruent such regions (eight octants, each subdivided by which of the three coordinates is largest), each of solid angle 4π/24. In it, θ∈[0,4π],φ∈[arctan(secθ),2π]. Here d=1/(2sinφcosθ), and the sinφ in the spherical measure sinφdφdθ cancels the sinφ in the denominator of d. Averaging over the region multiplies the integral by 24/(4π); together with the factor 21 in d=1/(2m) this gives the prefactor 3/π: E[d]=π3∫0π/4cosθ2π−arctan(secθ)dθ=π3∫0π/4cosθarctan(cosθ)dθ, where the last step uses arctan(x)+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[dcube]≈0.6107.■
The expected distance rises from 0.561 (2D) to 0.611 (3D) by just under 9%. An extra dimension opens longer body-diagonal directions (up to 3/2≈0.866), but the cube’s six faces cut most rays off quickly, so the uniform-direction average lifts only modestly above the inradius 21.
import numpy as np
np.random.seed(0); n = 10**6
# 2D: uniform theta, distance from centre of unit square to boundary.
theta = np.random.uniform(0, 2*np.pi, n)
dx, dy = np.cos(theta), np.sin(theta)
t = np.minimum(
np.minimum(np.where(dx>0, 0.5/dx, np.inf), np.where(dx<0, -0.5/dx, np.inf)),
np.minimum(np.where(dy>0, 0.5/dy, np.inf), np.where(dy<0, -0.5/dy, np.inf)))
exact_2d = (2/np.pi) * np.log(1 + np.sqrt(2))
print(f"2D sim: {t.mean():.5f} exact: {exact_2d:.5f}")
# 2D sim: 0.56113 exact: 0.56110
# 3D: uniform direction on sphere, distance from centre of unit cube to boundary.
theta = np.random.uniform(0, 2*np.pi, n)
u = np.random.uniform(-1, 1, n)
s = np.sqrt(1 - u*u)
dx, dy, dz = s*np.cos(theta), s*np.sin(theta), u
t = np.minimum.reduce([
np.where(dx>0, 0.5/dx, np.inf), np.where(dx<0, -0.5/dx, np.inf),
np.where(dy>0, 0.5/dy, np.inf), np.where(dy<0, -0.5/dy, np.inf),
np.where(dz>0, 0.5/dz, np.inf), np.where(dz<0, -0.5/dz, np.inf)])
print(f"3D sim: {t.mean():.5f} integral target: 0.61069")
# 3D sim: 0.61070