Two Random Points in a Unit Square: Distance at Most 1/2

A route to π/4 − 29/96 that skips the clipped-circle geometry: coordinate differences, their density, and a quarter-disk integral, plus a typo to watch for.

Binary Buddies · 5 min read · 5 views

Pick two points uniformly at random in the unit square. What is the probability that they are at most 1/2 apart? The obvious plan is to fix one point, compute the area of the radius-1/2 disk around it that lands inside the square, and average that area over all positions of the first point. That plan is correct, and it also turns into a painful integral. The asker of a recent Math StackExchange question reached 0.4833148 that way and confirmed it against a Python simulation, but could not get a closed form by hand.

The closed form is π/4 − 29/96, which is 0.4833148... to the digits quoted in the thread. Two answers there reach it without ever computing a clipped circle area. This article walks through that alternative proof, including the step in the accepted answer where a printed limit does not match the algebra that follows it, and says where the method stops working.

The idea in both answers is to stop thinking about where the points are and think only about how far apart their coordinates are. That turns a four-variable problem with a messy boundary into a two-variable problem over a quarter disk.

Why the direct approach is expensive

The asker puts the square in the first quadrant with its corner at the origin and takes one point at (h, k). Symmetry lets them restrict to h, k ≤ 1/2 and multiply by 4. In that quadrant the radius-1/2 circle around (h, k) can cross the left and bottom edges but never the right or top edges, since h + 1/2 ≤ 1. That is the reason for the restriction.

Even so, the area of the disk inside the square has to be built from an upper semicircle integral and a lower semicircle region with a rectangle subtracted from it. The lower half needs |g(x)| + g(x) inside the integrand to cut off the part of the circle below y = 0. The final expression is the area function A(h, k) integrated over h and k from 0 to 1/2 and multiplied by 4. That is three nested integrals with a square root and an absolute value, which is what the asker meant by 'extremely tedious'.

It works as a checking tool, and it matches the numeric value. As a way to derive a closed form it is the wrong shape of problem, because the clipping by the square's edges is baked into the integrand.

Replace the points with coordinate differences

Write the points as (x1, y1) and (x2, y2), with all four numbers independent and uniform on [0, 1]. The distance condition depends only on two quantities:

U = |x1 - x2|
V = |y1 - y2|
distance <= 1/2  <=>  U^2 + V^2 <= 1/4

Both answers start here. One of them says it took this starting point from a shared Gemini chat and did the rest by hand. The gain is that the edges of the square no longer appear in the condition. U and V each live on [0, 1], and the target region is a quarter disk of radius 1/2 centred at the origin of the (U, V) plane.

The cost is that (U, V) is not uniform on that square. Small differences are more likely than large ones, so the next job is to find the density of U, and so of V.

The density of |x1 − x2| from one area

The accepted answer gets the distribution of U from a picture. Plot (x1, x2) in the unit square. The event U > u is the set where |x1 − x2| > u, which is two right triangles in opposite corners, each with legs of length 1 − u. Their combined area is (1 − u)², so

P(U <= u) = 1 - (1 - u)^2 = 2u - u^2
f_U(u) = d/du (2u - u^2) = 2 - 2u = 2(1 - u)

No integral is needed for the CDF, only the area of two triangles and one derivative. The third answer reaches the same density by counting. For x1 ≤ x2 and a fixed gap u, x1 can range over [0, 1 − u] and x2 is then forced to equal x1 + u. That gives a weight of (1 − u). The case x1 > x2 gives the same weight, so the total is 2(1 − u).

The second route is less rigorous, since it treats 'relative probability' loosely, but it checks itself. The weights integrate to 1: ∫ from 0 to 1 of (2 − 2u) du = 2 − 1 = 1. The same argument applied to the y coordinates gives f_V(v) = 2(1 − v).

Independence gives the joint density

U depends only on x1 and x2. V depends only on y1 and y2. The two pairs are independent, so U and V are independent and the joint density is the product:

f(u, v) = 4(1 - u)(1 - v)    on [0,1] x [0,1]

This is the step that replaces the clipped-circle geometry. The edge effects are still present, but they are now encoded in the weighting factor (1 − u)(1 − v), which is largest at the origin and falls to zero at u = 1 or v = 1. Pairs that are close together in both coordinates get the most weight.

The probability is the integral of this density over the quarter disk u² + v² ≤ 1/4 with u, v ≥ 0. Both remaining routes evaluate that one integral, one in Cartesian coordinates and one in polar coordinates.

Cartesian route: sqrt, substitution, and two small integrals

The third answer sets up the integral as

P = int_{u=0}^{1/2} int_{v=0}^{sqrt(1/4 - u^2)} 4(1-u)(1-v) dv du

The inner integral in v is (4v − 2v²) evaluated at the upper limit, which gives 4·sqrt(1/4 − u²) − (1/2 − 2u²). What is left is (1 − u) times that, integrated over u from 0 to 1/2. Split it as A − B:

  • A is the integral of (1 − u)·4·sqrt(1/4 − u²). Substituting θ = arcsin(2u) turns it into the integral over 0 to π/2 of cos²θ − (sinθ cos²θ)/2. That evaluates to π/4 − 1/6.
  • B is the integral of (1 − u)(1/2 − 2u²), a plain polynomial. It evaluates to 13/96.

The difference is π/4 − 1/6 − 13/96. Since 1/6 = 16/96, that is π/4 − 29/96. The only non-polynomial step is the trigonometric substitution, which a strong high-school student can follow, though it takes some care with the cos³θ term.

Polar route, and a limit that does not match

The accepted answer switches to polar coordinates, u = r cosθ and v = r sinθ, with the extra factor r from the Jacobian. Expanding f_X(r cosθ)·f_Y(r sinθ)·r gives

4r - 4r^2 (cos t + sin t) + 4r^3 cos t sin t

Integrating over θ turns this into 2πr − 8r² + 2r³, and the r integral from 0 to 1/2 gives π/4 − 29/96. One thing to watch: the printed inner integral runs θ from 0 to π/4. That limit cannot produce the next line. A quarter disk needs θ from 0 to π/2. With π/2, the constant term gives 4r · π/2 = 2πr, the trig terms give 4r²·2 = 8r², and ∫ cosθ sinθ dθ = 1/2 gives 2r³. All three match the printed result, so the π/4 is a typo and the answer is unaffected. If you reproduce the derivation, use π/2 or you will be off by a factor of two in the leading term.

Finishing the r integral: 2π·(1/8) = π/4, then −8·(1/24) = −1/3, then 2·(1/64) = 1/32. Since −1/3 + 1/32 = −29/96, the total is π/4 − 29/96.

What the answer is made of, and how to check it

The first term, π/4, is the area of a disk of radius 1/2. If the points lived on an infinite plane with no edges, that would be roughly the shape of the answer. The −29/96 is the correction for the square's boundary, since pairs near the edges have part of their neighbourhood cut off. That reading matches the size of the correction: π/4 is about 0.785 and the true value is about 0.483.

The same polar integrand gives a general formula for any radius r ≤ 1, where the quarter disk still lies inside the unit square:

P(d <= r) = pi r^2 - (8/3) r^3 + (1/2) r^4

That is my own generalisation of the integral above, not something stated in the thread. Setting r = 1/2 reproduces π/4 − 1/3 + 1/32, which is the answer above. It is a useful consistency check because it should be monotone in r and reach 1 in the limit of a large enough radius, though the formula itself stops being valid once r exceeds 1.

For an empirical check, a plain Monte Carlo loop is enough:

import random
N = 1_000_000
hits = 0
for _ in range(N):
    x1, y1, x2, y2 = (random.random() for _ in range(4))
    if (x1 - x2)**2 + (y1 - y2)**2 <= 0.25:
        hits += 1
print(hits / N)

With a million samples the standard error is about sqrt(p(1 − p)/N), roughly 0.0005, so you should expect agreement with 0.4833 in the first two or three decimals and no further. A simulation cannot tell you whether the closed form is exactly right, only whether you have made a gross error.

Where this approach does not fit

The method depends on three facts. The coordinates are independent, each is uniform on an interval, and the distance condition separates into a function of |Δx| and |Δy|. Change any of these and it weakens. Points drawn from a disk or a triangle, for example, make the x and y coordinates dependent, so the joint density no longer factors and the (U, V) trick stops working.

The shape of the target region also matters. Here it is a quarter disk with its corner at the origin, which is why the polar form is clean. With a different metric, such as Manhattan distance, the region becomes a triangle and the integral is simpler. With a radius above 1 the quarter disk spills out of the unit square, and the formula above needs piecewise limits.

For a high-school audience, the Cartesian route in the third answer is the more accessible of the two, since it needs only one trigonometric substitution. It is also the more error-prone, because there are four or five separate pieces of arithmetic to keep straight. If you only need the number, the simulation in the asker's post is enough. If you need the exact value, the density of |x1 − x2| plus independence is the part to keep, because it replaces the clipped-circle geometry with one simple factor.

This article draws on What is the probability that two randomly chosen points inside a unit square are at a distance of at most $1/2$? and answers to it (1, 2), on the Stack Exchange network, licensed under CC BY-SA 4.0.

  • #alternative-proof
  • #geometric probability
  • #probability
  • #random points
  • #recreational-mathematics
  • #unit square

Comments

Add a comment
Sign in with Google to join the conversation