Library · Geometric Probability · Chapter 36

Distance between two points on the boundary of a square

Revised Report an error

Problem 36.1

Two points are chosen independently and uniformly on the boundary of the unit square. What is the expected distance between them?

Solution. Parametrise a boundary point by arclength s∈[0,4)s \in [0, 4) along the perimeter (starting from a corner, going counter-clockwise). Given two uniform points S1,S2S_1, S_2 on [0,4)[0, 4), we condition on which sides the two points lie on.

By symmetry, the 1616 side-pair combinations fall into three categories:

  • Same side (prob 416=14\tfrac{4}{16} = \tfrac14): distance is ∣X−Y∣|X - Y| for X,YX, Y iid uniform on [0,1][0, 1]. Expected value 13\tfrac13 (Chapter 33).

  • Adjacent sides (prob 816=12\tfrac{8}{16} = \tfrac12): WLOG one point at (X,0)(X, 0) and the other at (1,Y)(1, Y). Then with U=1−XU = 1 - X, E ⁣[U2+Y2]=2+ln⁡(1+2)3.\mathbb{E}\!\left[\sqrt{U^2 + Y^2}\right] = \frac{\sqrt{2} + \ln(1 + \sqrt{2})}{3}.

  • Opposite sides (prob 416=14\tfrac{4}{16} = \tfrac14): one point at (X,0)(X, 0) and the other at (Y,1)(Y, 1), distance (X−Y)2+1\sqrt{(X - Y)^2 + 1}. Then E ⁣[(X−Y)2+1]=2+3ln⁡(1+2)−23.\mathbb{E}\!\left[\sqrt{(X-Y)^2 + 1}\right] = \frac{2 + 3 \ln(1+\sqrt 2) - \sqrt{2}}{3}.

Combining by the law of total expectation: E[distance]=14⋅13+12⋅2+ln⁡(1+2)3+14⋅2+3ln⁡(1+2)−23.\begin{align*} \mathbb{E}[\text{distance}] &= \tfrac14 \cdot \tfrac13 + \tfrac12 \cdot \tfrac{\sqrt 2 + \ln(1+\sqrt 2)}{3} \\ &\quad + \tfrac14 \cdot \tfrac{2 + 3\ln(1+\sqrt 2) - \sqrt 2}{3}. \end{align*} A tidy algebraic simplification gives E[distance]=3+2+5ln⁡(1+2)12≈0.7351.■\boxed{\mathbb{E}[\text{distance}] = \frac{3 + \sqrt 2 + 5 \ln(1 + \sqrt 2)}{12} \approx 0.7351.} \qedhere

Compare with the related constant (2+ln⁡(1+2))/6≈0.3826(\sqrt 2 + \ln(1+\sqrt 2))/6 \approx 0.3826 (Chapter 34, distance of a uniform interior point to the centre). Both involve 2+ln⁡(1+2)\sqrt 2 + \ln(1+\sqrt 2), but the boundary-pair distance is larger, with a leading factor involving both the diagonal 2\sqrt 2 and the side-length contribution. Classical MSE exercise.

import numpy as np
u = np.random.rand(10**7) * 4  # position on perimeter
v = np.random.rand(10**7) * 4
def to_xy(s):
    x = np.where(s < 1, s,
        np.where(s < 2, 1,
        np.where(s < 3, 3 - s, 0)))
    y = np.where(s < 1, 0,
        np.where(s < 2, s - 1,
        np.where(s < 3, 1, 4 - s)))
    return x, y
x1, y1 = to_xy(u); x2, y2 = to_xy(v)
d = np.sqrt((x1-x2)**2 + (y1-y2)**2)
exact = (3 + np.sqrt(2) + 5*np.log(1 + np.sqrt(2))) / 12
print(f"sim: {d.mean():.5f}   exact: {exact:.5f}")
# sim: 0.73485   exact: 0.73509
Report an error on this page

Reports are stored by Netlify. See the privacy note.