Library · Geometric Probability · Chapter 36
Distance between two points on the boundary of a square
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 along the perimeter (starting from a corner, going counter-clockwise). Given two uniform points on , we condition on which sides the two points lie on.
By symmetry, the side-pair combinations fall into three categories:
Same side (prob ): distance is for iid uniform on . Expected value (Chapter 33).
Adjacent sides (prob ): WLOG one point at and the other at . Then with ,
Opposite sides (prob ): one point at and the other at , distance . Then
Combining by the law of total expectation: A tidy algebraic simplification gives
Compare with the related constant (Chapter 34, distance of a uniform interior point to the centre). Both involve , but the boundary-pair distance is larger, with a leading factor involving both the diagonal 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