Skip to content
Vamshi Jandhyala

Books · Jane Street: Solutions

Chapter 5

Some Off Square

↓ Download PDF handout

A geometric-probability puzzle.

Problem

A circle is generated at random by sampling two points uniformly and independently from the interior of a square and using these points as the endpoints of a diameter. What is the probability that part of the circle lies outside the square?

Solution

Let P1P_1 and P2P_2 be the two sampled points and MM their midpoint. The random circle has centre MM and radius MP1=MP2|MP_1| = |MP_2|, and it crosses the boundary of the square exactly when the distance from MM to that boundary is strictly less than the radius.

Take the square to be [1,1]2[-1, 1]^2 and the two points to be P1=(x1,y1)P_1 = (x_1, y_1) and P2=(x2,y2)P_2 = (x_2, y_2) with each coordinate drawn independently from U[1,1]\mathcal{U}[-1, 1]. The midpoint is M=12(x1+x2,y1+y2)M = \tfrac{1}{2}(x_1 + x_2, \, y_1 + y_2) and its distance to the nearest side of the square is min ⁣(1x1+x22,  1y1+y22).\min\!\Bigl(\, 1 - \bigl|\tfrac{x_1 + x_2}{2}\bigr|, \; 1 - \bigl|\tfrac{y_1 + y_2}{2}\bigr| \,\Bigr). The radius is half the chord length: 12(x1x2)2+(y1y2)2.\frac{1}{2} \sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}. The event of interest is therefore min ⁣(1x1+x22,  1y1+y22)    12(x1x2)2+(y1y2)2.\min\!\Bigl(\, 1 - \bigl|\tfrac{x_1 + x_2}{2}\bigr|, \; 1 - \bigl|\tfrac{y_1 + y_2}{2}\bigr| \,\Bigr) \;\leq\; \frac{1}{2} \sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}.

Computational solution

A Monte Carlo simulation over ten million trials gives the probability as p    0.476.p \;\approx\; \mathbf{0.476}.

Python code

from random import uniform
from math import sqrt

runs = 10_000_000
cnt = 0
for _ in range(runs):
    x_1, x_2, y_1, y_2 = (
        uniform(-1, 1), uniform(-1, 1),
        uniform(-1, 1), uniform(-1, 1),
    )
    if min(2 - abs(x_1 + x_2),
           2 - abs(y_1 + y_2)) \
       <= sqrt((x_1 - x_2)**2 + (y_1 - y_2)**2):
        cnt += 1
print(cnt / runs)