Simulating Continuous Experiments
Some experiments have a continuum of outcomes: the point where a dart lands, the time until a light bulb fails, the angle at which a needle falls. Any single exact value then has probability 0, so probabilities belong to regions. Simulation makes this concrete: a random point is uniform in a region when the chance of landing in any subregion is proportional to its area.
Random points and areas
rng.random() gives a number uniform on . Two of them give a point uniform in the unit square. The fraction of points that land in a region estimates its area. Use this to estimate from the quarter disc , whose area is :
import random
rng = random.Random(8)
for n in [1_000, 10_000, 100_000, 1_000_000]:
inside = sum(1 for _ in range(n) if rng.random() ** 2 + rng.random() ** 2 <= 1)
print(f"n={n:8d} pi ~ {4 * inside / n:.4f}")n= 1000 pi ~ 3.1920
n= 10000 pi ~ 3.1552
n= 100000 pi ~ 3.1520
n= 1000000 pi ~ 3.1386
Accuracy improves slowly, like , as for any simulated probability. Monte Carlo integration is useless for but invaluable in high dimensions, where grids are hopeless (Monte Carlo Methods).
Buffon's needle
Drop a needle of length 1 on a floor with parallel lines 1 unit apart. Let be the distance from the needle's centre to the nearest line and the angle between needle and lines; both are uniform. The needle crosses a line when . The probability is the area under that curve divided by the area of the rectangle:
import math
import random
rng = random.Random(9)
n = 200_000
cross = 0
for _ in range(n):
d = rng.uniform(0, 0.5)
theta = rng.uniform(0, math.pi / 2)
cross += d <= 0.5 * math.sin(theta)
p = cross / n
print("P(cross) ~", round(p, 4), " exact 2/pi =", round(2 / math.pi, 4))
print("pi ~", round(2 / p, 4))P(cross) ~ 0.6358 exact 2/pi = 0.6366
pi ~ 3.1459
(Using math.pi to simulate the angle and then "estimate" is circular, but the probability itself is a genuine prediction checked by the simulation.)
Probability as an integral
The geometric examples share one structure: an outcome is a point in a region , and . When outcomes are not uniform, area is replaced by an integral of a density function, the subject of the next lesson. Simulation still works by turning uniform numbers into other distributions.
import random
# Two friends arrive uniformly at random between 12:00 and 13:00
# and each waits 15 minutes. Do they meet?
rng = random.Random(10)
n = 200_000
meet = sum(1 for _ in range(n) if abs(rng.random() - rng.random()) <= 0.25)
print("simulated:", meet / n, " exact:", 1 - 0.75 ** 2)simulated: 0.437295 exact: 0.4375
In the unit square of arrival times, the friends fail to meet in two triangles with legs , total area , so they meet with probability .
In software
Random points in shapes are used in rendering (path tracing samples light directions), in robotics (sampling-based motion planners such as RRT), in games (random spawn locations) and in testing geometry code. Generating points uniformly in a disc is a classic trap: picking a uniform radius crowds points near the centre; use or rejection from the square.
Exercises
Exercise 1: A dart
A dart lands uniformly on a square board of side 2 with a circle of radius 1 painted in the middle. What is the probability it lands outside the circle?
Answer
.
Solution
Circle area , board area 4: inside with probability .
Exercise 2: Meeting with a shorter wait
In the meeting problem, each friend waits only 10 minutes. What is the probability they meet?
Answer
.
Solution
Waiting time hour: .
Exercise 3: Uniform points in a disc (code)
Generate points in the unit disc with (wrong) and (right), with uniform angle. Compare the fraction landing within radius with the correct value .
Answer
The wrong method gives about , the right one about .
Solution
The disc of radius has a quarter of the area. With , ; with , .
import random
rng = random.Random(12)
n = 100_000
wrong = sum(rng.random() <= 0.5 for _ in range(n)) / n
right = sum(rng.random() ** 0.5 <= 0.5 for _ in range(n)) / n
print(wrong, right)0.50101 0.2492
The angle does not affect the radius test, so only the radius is simulated.
Test yourself
For a point uniform in the unit square, what is exactly?
- 0.5
- 0
- 0.25
- Undefined
In Buffon's needle with needle length equal to line spacing, the crossing probability is…