Books · Jane Street: Solutions
Chapter 5
Some Off Square
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 and be the two sampled points and their midpoint. The random circle has centre and radius , and it crosses the boundary of the square exactly when the distance from to that boundary is strictly less than the radius.
Take the square to be and the two points to be and with each coordinate drawn independently from . The midpoint is and its distance to the nearest side of the square is The radius is half the chord length: The event of interest is therefore
Computational solution
A Monte Carlo simulation over ten million trials gives the probability as
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)