TExcelTAD
Home › Probability Theory › Simulating Continuous Experiments

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 [0,1)[0, 1). 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 π\pi from the quarter disc x2+y2≤1x^2 + y^2 \le 1, whose area is π/4\pi/4:

python · try it yourself
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 1/n1/\sqrt{n}, as for any simulated probability. Monte Carlo integration is useless for π\pi 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 d∈[0,1/2]d \in [0, 1/2] be the distance from the needle's centre to the nearest line and θ∈[0,π/2]\theta \in [0, \pi/2] the angle between needle and lines; both are uniform. The needle crosses a line when d≤12sin⁡θd \le \frac{1}{2} \sin\theta. The probability is the area under that curve divided by the area of the rectangle:

Pr⁡[cross]=∫0π/212sin⁡θ dθ12⋅π2=1/2π/4=2π≈0.6366\Pr[\text{cross}] = \frac{\int_0^{\pi/2} \frac{1}{2}\sin\theta \, d\theta}{\frac{1}{2} \cdot \frac{\pi}{2}} = \frac{1/2}{\pi/4} = \frac{2}{\pi} \approx 0.6366
python · try it yourself
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" π\pi 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 xx in a region Ω\Omega, and Pr⁡[E]=area(E)area(Ω)\Pr[E] = \frac{\text{area}(E)}{\text{area}(\Omega)}. 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.

python · try it yourself
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 0.750.75, total area 0.752=0.56250.75^2 = 0.5625, so they meet with probability 0.43750.4375.

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 r=ur = \sqrt{u} 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

1−π/4≈0.2151 - \pi/4 \approx 0.215.

Solution

Circle area π\pi, board area 4: inside with probability π/4\pi/4.

Exercise 2: Meeting with a shorter wait

In the meeting problem, each friend waits only 10 minutes. What is the probability they meet?

Answer

11/36≈0.30611/36 \approx 0.306.

Solution

Waiting time 1/61/6 hour: 1−(5/6)2=11/361 - (5/6)^2 = 11/36.

Exercise 3: Uniform points in a disc (code)

Generate points in the unit disc with r=ur = u (wrong) and r=ur = \sqrt{u} (right), with uniform angle. Compare the fraction landing within radius 1/21/2 with the correct value 1/41/4.

Answer

The wrong method gives about 0.50.5, the right one about 0.250.25.

Solution

The disc of radius 1/21/2 has a quarter of the area. With r=ur = u, Pr⁡[r≤1/2]=1/2\Pr[r \le 1/2] = 1/2; with r=ur = \sqrt{u}, Pr⁡[u≤1/2]=Pr⁡[u≤1/4]=1/4\Pr[\sqrt{u} \le 1/2] = \Pr[u \le 1/4] = 1/4.

python · try it yourself
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 P(x=0.5)P(x = 0.5) exactly?

  1. 0.5
  2. 0
  3. 0.25
  4. Undefined

In Buffon's needle with needle length equal to line spacing, the crossing probability is…

  1. 1/21/2
  2. 2/π2/\pi
  3. π/4\pi/4
  4. 1/π1/\pi