Monte Carlo Explorer
Random sampling for estimation and numerical integration — run experiments and watch confidence intervals accumulate
© 2026 Theodore P. Pavlic
· MIT License
● inside circle   ✕ outside circle
Total samples
Inside circle
Estimate of π/4
Estimate of π
True π/4
0.78539816339744…
True π
3.14159265358979…
Each "dart" lands at a uniform random point in the square. The fraction landing inside the inscribed circle estimates π/4, so four times that fraction estimates π. Run many experiments to watch 95% confidence intervals for π accumulate — nearly all should capture the dashed true value.
Experiment History — 95% Confidence Intervals for π
Why does dartboard sampling estimate π?
Area of square:(2r)² = 4r²
Area of circle:πr²
Pr(dart inside):πr² / 4r² = π/4
By LLN as N → ∞:(# inside) / N  →  π/4
Therefore: π ≈ 4 × (# inside circle) / (# total samples)
Borel’s Law of Large Numbers guarantees convergence — the 95% CI will capture π ≈ 95% of the time.
π is four times π/4, and nothing else changes: the chart above plots the estimate of π/4 and both of its interval endpoints multiplied by 4.
crosses a line   no crossing
Total needles
Crossing a line
Estimate of 2/π
Estimate of π
True 2/π
0.63661977236758…
True π
3.14159265358979…
Each needle lands at a uniform random position and angle on paper ruled with lines exactly one needle-length apart. The fraction crossing a line estimates 2/π, so dividing 2 by that fraction estimates π. Run many experiments to watch 95% confidence intervals for π accumulate — nearly all should capture the dashed true value.
Experiment History — 95% Confidence Intervals for π
Why does dropping needles estimate π?
Needle angle:θ ~ U[0, π)
Center to nearest line:d ~ U[0, t/2]
Needle crosses when:d ≤ (ℓ/2)·sin θ
Mean half-span:avg of (ℓ/2)·sin θ = ℓ/π
Pr(needle crosses):(ℓ/π) ÷ (t/2) = 2ℓ/(tπ)
By LLN as N → ∞:(# crossings) / N → 2ℓ/(tπ)
Therefore: π ≈ 2 × (# needles dropped) / (# crossing a line)
Two averages give the result: sin θ averages 2/π over [0, π), and d is uniform on [0, t/2], so a needle crosses with probability 2ℓ/(tπ). This tab always uses ℓ = t (needles as long as the line spacing, as matchsticks are on ruled paper), which reduces that probability to 2/π.
π sits in the denominator here, unlike the dartboard on Tab ①. The estimate is therefore a ratio, slightly biased at finite N, and its interval comes from inverting the interval for the crossing probability, which swaps the two endpoints.
Buffon’s needle extension: the same count, read backwards, measures area
Two sets of randomly scattered lines of total lengths S and L that are thrown across a region of area A cross each other about 2SL/(πA) times. The ruled sheet above is the case where one of the two sets is the ruling itself. Inside a region of area A, lines spaced t apart have a total length of A/t, and the general count then collapses back to the 2ℓ/(tπ) per needle that this tab counts.
Crossings expected:N ≈ 2SL/(πA)
Solve it for π:π ≈ 2SL/(AN) – the area is known, and so counting crossings measures π
Solve it for A:A ≈ 2SL/(πN) – π is known, and so counting crossings measures the area
Mallon and Franks (2000) showed that crevice-dwelling Temnothorax albipennis ants may be sizing up new nest candidates using a method that works because of the relationship above. Scout ants enter the nest and lay down a random trail on its floor, acting like the first set of randomly scattered lines. The next time the scout enters the nest, she can count how often she crosses her previous trail. By the expression above, that count can be used to estimate the area of the cavity. A robot with an odometer and no map can do the same thing, which allows for estimating area without energetically and computationally costly sensing systems like LIDAR.
A robot pacing a cavity it cannot see
The robot fixes how far it will walk before it sets out. It paces the cavity for that distance with its pen down, leaves, and paces it again in a second color. Every crossing between the two trails is marked, and the count alone gives the floor area: the robot never measures a wall and never sees the room. The animation works through four prescribed distances in turn, and each finished pair drops one estimate into that distance’s own row on the right, where the rows show what distance buys. The cavity’s floor is 875 mm², the same as the standard nest in the ant study, where a scout’s median first visit covered 486 mm.
cavity floor: 875 mm², unknown to the robot planned distance: 100 mm each pass
Loading…
Estimates by prescribed path length
one pair of walks the pair just shown mean and its 95% interval true area, 875 mm²
Because the robot picks its distance before it sets out, every walk in a row is exactly that long and their estimates pool. The rows differ in nothing else.
Tab ④ reaches an area a different way. Monte Carlo integration samples points across a domain that is already known and averages the height of a function there, which measures the area under a curve. The robot has no map to sample from, and so it counts crossings instead. Both recover an area from nothing but random samples.
Random rectangles of width L/M and height f(xi)
Samples (M)
Domain length (L)
Rect. width (L/M)
MC Estimate
True Integral
Relative Error
Estimator: (L/M) · Σ f(xi), xi ~ U[0,π]
This run:
Estimate:
Exact value: —
① Connection: this function's integral equals π/4 — the same quantity estimated by dartboard sampling on Tab ①. Both are Monte Carlo estimates of the same number!
Experiment History — 95% Confidence Intervals for ∫f(x)dx
Random Riemann Rectangles — as M grows, width L/M shrinks
Each rectangle has width L/M and height f(xi) at a random xi ~ U(a, b). As M → ∞, overlaps and gaps average out and the estimate converges to the true integral. The formula (L/M)·Σf(xi) is a Riemann sum with random partition points. This animation always uses f(x) = sin(x) on [0, π].
Loading…