Skip to content
Vamshi Jandhyala

Books · The Riddler

Chapter 92

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)(0, 0), (1,0)(1, 0), (1,1)(1, 1), (0,1)(0, 1). By symmetry, the mean distance over all directions equals the mean over the triangle with vertices (1/2,1/2)(1/2, 1/2), (1,1/2)(1, 1/2), (1,1)(1, 1). Pick a direction θ[0,π/4]\theta \in [0, \pi/4]; the distance to the right edge is d=1/(2cosθ)d = 1/(2 \cos \theta).

Weighting by the uniform density on the fundamental sector, E[d]  =  0π/412cosθ4πdθ  =  2π0π/4secθdθ.\mathbb{E}[d] \;=\; \int_0^{\pi/4} \frac{1}{2 \cos \theta} \cdot \frac{4}{\pi} \, d\theta \;=\; \frac{2}{\pi} \int_0^{\pi/4} \sec \theta \, d\theta. The antiderivative of secθ\sec \theta is lnsecθ+tanθ\ln|\sec \theta + \tan \theta|, so E[d]  =  2πln(2+1)  =  ln(3+22)π    0.5611.\mathbb{E}[d] \;=\; \frac{2}{\pi} \, \ln(\sqrt 2 + 1) \;=\; \frac{\ln(3 + 2\sqrt 2)}{\pi} \;\approx\; \boxed{0.5611}.

Why the secxdx\int \sec x \, dx step works: four derivations

Method 1: multiplication by a clever form of 1.

Multiply by (secx+tanx)/(secx+tanx)=1(\sec x + \tan x)/(\sec x + \tan x) = 1: secxdx  =  sec2x+secxtanxsecx+tanxdx.\int \sec x \, dx \;=\; \int \frac{\sec^2 x + \sec x \tan x}{\sec x + \tan x} \, dx. With u=secx+tanxu = \sec x + \tan x, the numerator equals dudu, so the integral equals lnu+C=lnsecx+tanx+C\ln|u| + C = \ln|\sec x + \tan x| + C.

Method 2: Weierstrass substitution.

With t=tan(x/2)t = \tan(x/2), the half-angle formulas give sinx=2t/(1+t2)\sin x = 2t/(1+t^2), cosx=(1t2)/(1+t2)\cos x = (1-t^2)/(1+t^2), and dx=2dt/(1+t2)dx = 2 \, dt/(1+t^2). Then secxdx  =  21t2dt  =  ln ⁣1+t1t+C  =  ln ⁣tan ⁣(π4+x2)+C.\int \sec x \, dx \;=\; \int \frac{2}{1 - t^2} \, dt \;=\; \ln\!\left|\frac{1 + t}{1 - t}\right| + C \;=\; \ln\!\left|\tan\!\bigl(\tfrac{\pi}{4} + \tfrac{x}{2}\bigr)\right| + C.

Method 3: complex exponentials.

Since cosx=(eix+eix)/2\cos x = (e^{ix} + e^{-ix})/2, secxdx  =  2eixe2ix+1dx.\int \sec x \, dx \;=\; \int \frac{2 \, e^{ix}}{e^{2ix} + 1} \, dx. Substitute u=eixu = e^{ix}, giving 2/(i(u2+1))du=2iarctan(eix)+C\int 2/(i(u^2 + 1)) \, du = -2i \arctan(e^{ix}) + C, which on the real line reduces to lnsecx+tanx+C\ln|\sec x + \tan x| + C.

Method 4: via hyperbolic functions.

Setting x=itx = it, cos(it)=cosht\cos(it) = \cosh t, so secxdx  =  isechtdt  =  2iarctan(tanh(t/2))+C,\int \sec x \, dx \;=\; i \int \operatorname{sech} t \, dt \;=\; 2i \arctan\bigl(\tanh(t/2)\bigr) + C, which again equals lnsecx+tanx+C\ln|\sec x + \tan x| + C after substituting t=ixt = -i x.

Monte Carlo verification

import numpy as np

def simulate_random_walk_2d(num_simulations=1_000_000):
    """Simulate random walks from the centre of the unit square."""
    x0, y0 = 0.5, 0.5
    theta = np.random.uniform(0, 2 * np.pi, num_simulations)
    dx, dy = np.cos(theta), np.sin(theta)

    t_right  = np.where(dx > 0, (1 - x0) / dx, np.inf)
    t_left   = np.where(dx < 0,  -x0 / dx,     np.inf)
    t_top    = np.where(dy > 0, (1 - y0) / dy, np.inf)
    t_bottom = np.where(dy < 0,  -y0 / dy,     np.inf)

    return np.minimum(np.minimum(t_right, t_left),
                      np.minimum(t_top,   t_bottom))

np.random.seed(42)
exact = np.log(3 + 2 * np.sqrt(2)) / np.pi
d = simulate_random_walk_2d(10**6)
print(f"analytical: {exact:.6f}")
print(f"simulated:  {d.mean():.6f}")
# analytical: 0.561100
# simulated:  0.561103

Riddler Classic

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)(0, 0, 0) and (1,1,1)(1, 1, 1). A random direction on the sphere is written as v=(sinφcosθ,sinφsinθ,cosφ)\vec v = (\sin \varphi \cos \theta, \sin \varphi \sin \theta, \cos \varphi) with θ[0,2π)\theta \in [0, 2\pi) and φ[0,π]\varphi \in [0, \pi]; the uniform measure on the sphere is (4π)1sinφdφdθ(4\pi)^{-1} \sin \varphi \, d\varphi \, d\theta. From the centre, the distance along v\vec v to the boundary is d  =  12m,m  =  max(sinφcosθ,  sinφsinθ,  cosφ).d \;=\; \frac{1}{2 \, m}, \qquad m \;=\; \max\bigl(|\sin \varphi \cos \theta|,\; |\sin \varphi \sin \theta|,\; |\cos \varphi|\bigr).

The cube’s direction sphere is cut into congruent pieces by its symmetries: 88 octants from the sign choices, and within each octant 33 regions according to which coordinate is largest in magnitude. Restrict to the region in the first octant where the xx-component is largest (the yy and zz order is left free), which is therefore 124\tfrac{1}{24} of the sphere: θ[0,π4],φ[arctan(secθ),π2].\theta \in \left[0, \tfrac{\pi}{4}\right], \qquad \varphi \in \left[\arctan(\sec \theta),\, \tfrac{\pi}{2}\right]. In this region, d=1/(2sinφcosθ)d = 1/(2 \sin \varphi \cos \theta), and the sinφ\sin \varphi in the measure cancels the sinφ\sin \varphi in the denominator of dd. Multiplying by 2424 and dividing by 4π4\pi: E[d]  =  3π0π/4π2arctan(secθ)cosθdθ  =  3π0π/4arctan(cosθ)cosθdθ,\begin{aligned} \mathbb{E}[d] &\;=\; \frac{3}{\pi} \int_0^{\pi/4} \frac{\tfrac{\pi}{2} - \arctan(\sec \theta)}{\cos \theta} \, d\theta\\ &\;=\; \frac{3}{\pi} \int_0^{\pi/4} \frac{\arctan(\cos \theta)}{\cos \theta} \, d\theta, \end{aligned} using arctanx+arctan(1/x)=π/2\arctan x + \arctan(1/x) = \pi/2 for x>0x > 0.

The integral does not collapse to elementary constants. Numerically, 0π/4arctan(cosθ)cosθdθ    0.63951,\int_0^{\pi/4} \frac{\arctan(\cos \theta)}{\cos \theta} \, d\theta \;\approx\; 0.63951, giving E[d]    0.6106.\mathbb{E}[d] \;\approx\; \boxed{0.6106}.

Distance to each face

From the centre (1/2,1/2,1/2)(1/2, 1/2, 1/2), the distance to each face in direction (sinφcosθ,sinφsinθ,cosφ)(\sin \varphi \cos \theta, \sin \varphi \sin \theta, \cos \varphi) is:

  • right (x=1x = 1): t=1/(2sinφcosθ)t = 1 / (2 \sin \varphi \cos \theta) if sinφcosθ>0\sin \varphi \cos \theta > 0

  • left (x=0x = 0): t=1/(2sinφcosθ)t = 1 / (2 |\sin \varphi \cos \theta|) if sinφcosθ<0\sin \varphi \cos \theta < 0

  • front (y=1y = 1): t=1/(2sinφsinθ)t = 1 / (2 \sin \varphi \sin \theta) if sinφsinθ>0\sin \varphi \sin \theta > 0

  • back (y=0y = 0): t=1/(2sinφsinθ)t = 1 / (2 |\sin \varphi \sin \theta|) if sinφsinθ<0\sin \varphi \sin \theta < 0

  • top (z=1z = 1): t=1/(2cosφ)t = 1 / (2 \cos \varphi) if cosφ>0\cos \varphi > 0

  • bottom (z=0z = 0): t=1/(2cosφ)t = 1 / (2 |\cos \varphi|) if cosφ<0\cos \varphi < 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
10310^3 0.61330.6133 0.00530.0053
10410^4 0.61090.6109 0.00170.0017
10510^5 0.61050.6105 0.000530.00053
10610^6 0.61060.6106 0.000170.00017
10710^7 0.610600.61060 0.000050.00005
Integral 0.61060.6106

Comparison of the two answers

Dimension Exact form Numerical
22D (square) ln(3+22)π\dfrac{\ln(3 + 2 \sqrt 2)}{\pi} 0.56110.5611
33D (cube) 3π0π/4arctan(cosθ)cosθdθ\dfrac{3}{\pi} \displaystyle \int_0^{\pi/4} \dfrac{\arctan(\cos \theta)}{\cos \theta} \, d\theta 0.61060.6106

The expected distance grows only modestly (by about 9%9\%) from 22D to 33D. Although an extra dimension opens longer “body-diagonal” directions (up to 3/20.866\sqrt 3 / 2 \approx 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.50.5.