← All Labs
INTERACTIVE APPLET · PROBABILITY · MONTE CARLO INTEGRATION

Monte Carlo Estimating π

Throw a fistful of darts, blindfolded, at a square dartboard with a quarter-circle painted on it. Each dart lands somewhere at random. Count what fraction land inside the arc — and that fraction, multiplied by four, is π. No trigonometry, no infinite series, no clever formula: just counting hits. The same trick estimates any area or integral, including ones with no closed form at all — you fence off a region, rain random points into its bounding box, and the hit-rate times the box area is the answer. Watch a deterministic constant assemble itself out of pure chance, and watch the error shrink like 1/√N.

π ≈ 4 · (darts inside the quarter-circle) / (darts thrown)

The dartboard integral — chance computes a constant

Rain random points · count the fraction inside · scale by the box area
p5.js · live random sampling

DRIVE IT

Dart inside the region (a hit) Dart outside (a miss) Region boundary True value / error trace
Press Rain to release a stream of random darts. Every dart is an independent uniform guess; the running hit-fraction is your estimate, drawn one point at a time.
CURRENT ESTIMATE OF π
4 × inside / total, live

WALKTHROUGH

  1. Stay in mode and hit Rain. Green darts land inside the quarter-circle, grey ones outside. The big number is 4 × inside/total — watch it wobble around 3.14159…
  2. Let it run to a few thousand darts, then hit Burst 50,000. The estimate tightens toward π. The scatter is the picture of a probability: density ∝ area.
  3. Switch to mode and pick y = e^(−x²) from the dropdown. This integral has no elementary closed form, yet the darts compute it anyway — hit-fraction × box area = the area under the curve.
  4. Try the heart region. There's no formula to look up, but Monte Carlo doesn't care what the shape is — it just needs a yes/no "am I inside?" test.
  5. Open mode . The red guide line has slope −½ on log–log axes: to get one more correct digit you need 100× the darts. Slow, but it works in any dimension — that's why physics and finance lean on it.

AHA

THE INSIGHT
A precise, deterministic number — π, or an integral with no formula — falls out of pure randomness. The fraction of darts that land in a region is that region's share of the area, because uniform points are as likely to land anywhere. So counting hits measures geometry. The catch is the price: the error shrinks only like 1/√N, so accuracy is expensive — but it barely gets worse as dimensions pile up, which is exactly where every other method drowns.

RESEARCH NOTE

The estimator is unbiased and its standard error falls as \(\sigma/\sqrt{N}\) — independent of the dimension, unlike grid quadrature whose cost explodes as \(h^{-d}\). Further reading: Metropolis & Ulam, The Monte Carlo Method (1949); Buffon's needle (1777) — the first Monte Carlo estimate of π; and quasi-Monte Carlo / low-discrepancy sequences, which beat \(1/\sqrt N\) for smooth integrands.

EXPLANATION

Put a quarter-circle of radius 1 inside the unit square \([0,1]\times[0,1]\). The square has area 1; the quarter-circle has area \(\pi/4\). Now scatter points uniformly at random across the square. Because the points are uniform, the probability that any one lands inside the quarter-circle equals the region's fraction of the total area — exactly \(\pi/4\). So if you throw \(N\) darts and \(N_{\text{in}}\) land inside (i.e. satisfy \(x^2+y^2\le 1\)), the ratio \(N_{\text{in}}/N\) estimates \(\pi/4\), and \(\pi \approx 4\,N_{\text{in}}/N\). Nothing about circles or angles is used — only counting and the definition of area as "how much of the whole a shape occupies."

The same idea integrates anything. To find \(\int_a^b f(x)\,dx\) for a non-negative \(f\), enclose the graph in a bounding box of known area \(A_{\text{box}}\), rain darts into the box, and count the fraction that land under the curve (the test is simply \(y \le f(x)\)). Then the area under \(f\) is \(A_{\text{box}}\cdot(N_{\text{in}}/N)\). This is why Monte Carlo shrugs at functions with no closed-form antiderivative, like \(e^{-x^2}\): it never needs a formula, only the ability to ask "is this point inside?". Swap the circle for a heart, a fractal, or a tangle of inequalities in a hundred variables and the recipe is unchanged.

What does it cost? Each dart is a Bernoulli trial with success probability \(p\) (the true area fraction). The count \(N_{\text{in}}\) is binomial, so the estimate \(\hat p = N_{\text{in}}/N\) has standard deviation \(\sqrt{p(1-p)/N}\). That is the crucial law: the typical error decays like \(\boldsymbol{1/\sqrt{N}}\). To halve the error you need four times as many darts; to win one more decimal digit you need a hundred times as many. On a log–log plot of error against \(N\) the points ride a line of slope \(-\tfrac12\) — you can watch that line assemble itself in mode ③. This slow convergence is Monte Carlo's price of admission, and it is why estimating π this way is charming but wildly inefficient compared to a series.

So why is the method everywhere — pricing options, simulating reactor neutrons, rendering film lighting, sampling Bayesian posteriors? Because that \(1/\sqrt{N}\) rate is blind to dimension. A grid method that places points every \(h\) apart needs \(h^{-d}\) of them in \(d\) dimensions — the "curse of dimensionality" makes it hopeless past a handful of variables. Monte Carlo's error depends on the variance of the integrand and the number of samples, not on \(d\). In ten or ten-thousand dimensions the darts still work, just as they do on our little dartboard. Randomness, it turns out, is not the enemy of precision — with enough samples it is a perfectly good way to compute the most deterministic quantities we know.