An interactive explainer

Throwing darts at π

How random numbers can calculate π, why that works more slowly than you'd hope, and how the same trick runs in reverse to turn measurements into models. Made with assistance of Claude Opus 5.5 (Anthropic). Use at your own peril.

01

The idea

Draw a square that is 1 unit on each side. Put the centre of a circle of radius 1 at the square's bottom-left corner. Exactly one quarter of that circle fits inside the square.

The square has area 1 × 1 = 1. A full circle of radius 1 has area π × 1² = π, so the quarter circle has area π/4 ≈ 0.785.

Now throw darts so that each one lands at a completely random spot in the square, with every spot equally likely. The chance that a dart lands inside the quarter circle is just the share of the square's area the quarter circle covers: π/4. So after lots of throws,

darts inside ÷ darts thrown ≈ π/4
⇒ π ≈ 4 × (darts inside ÷ darts thrown)

Checking whether a dart at position (x, y) is inside is easy with Pythagoras: its distance from the corner is √(x² + y²), so it's inside when x² + y² ≤ 1. Notice that nowhere in this recipe do you need to already know π.

r = 1 area = π/4 area = 1 − π/4 0 1 1 x y

inside: x² + y² ≤ 1outside

02

Throw some darts

Throw one dart at a time to see the randomness, or let it run and watch the estimate settle. The chart underneath tracks your estimate as the number of darts grows.

landed insidelanded outside
Current estimate of π—
π ≈ 4 × inside ÷ thrown
Darts thrown
0
Landed inside
0
Off from true π by
—
Typical error at this many darts
—

True value: π = 3.14159265…

How the estimate settles down

your running estimate where about 68% of runs would be where about 95% of runs would be

The x-axis is logarithmic: each gridline is ten times more darts than the last. The shaded funnel shows how much a typical estimate wobbles at each stage. Early on it swings wildly; by a hundred thousand darts it is usually within about 0.005 of π.

03

Why it's slow: the 1/√N rule

Each dart is basically a coin flip that comes up "inside" with probability π/4. Statistics tells us how much the average of N such flips wobbles: the typical error in the π estimate is about

typical error ≈ 4 × √( (π/4)(1 − π/4) / N ) ≈ 1.64 ÷ √N

The important part is the √N. To make the error 10 times smaller you need 100 times more darts. A million darts gets you roughly three correct decimal places; each extra digit costs another factor of 100.

You can see this directly by repeating the whole experiment many times. Pick how many darts each experiment uses, run 1,000 experiments, and look at how spread out the 1,000 answers are. Try a few different sizes and compare the table.

share of the 1,000 experiments giving each estimate bell curve predicted by the 1.64/√N rule (N ≥ 100)
Darts per experiment (N)Average of the 1,000 estimatesMeasured spread (std. dev.)Predicted spread, 1.64 ÷ √N

Two things to notice. The average lands on π no matter how small N is (the method is unbiased). Only the spread changes, and it shrinks like 1/√N. With only 10 darts the answers can only be 0, 0.4, 0.8 … 4.0, which is why that histogram looks like a comb.

04

What makes it "Monte Carlo"

The name comes from the casino in Monaco. It was coined by scientists at Los Alamos in the 1940s (Stanislaw Ulam, John von Neumann and Nicholas Metropolis) who used random sampling to study neutrons in nuclear reactions. Every Monte Carlo method follows the same recipe:

  1. Define the space of possibilities (every point in the square).
  2. Sample it at random (throw darts).
  3. Do a simple, deterministic check on each sample (is x² + y² ≤ 1?).
  4. Average the results to estimate the answer (the fraction inside, times 4).

The trick is turning a question with an exact answer (what is this area?) into an average over random samples, which is often far easier to compute.

Why bother, if it's so slow?

For π alone, there are vastly faster formulas. Monte Carlo earns its keep in problems with many dimensions. If you estimate an area with a grid of 10 points per side, two dimensions need 100 points, but 20 dimensions need 10²⁰. The Monte Carlo error rule, 1.64/√N, has no dimension in it at all: the cost of a given accuracy doesn't explode as you add dimensions.

That is why it's used for pricing financial risk, simulating particles and materials, rendering realistic light in films (path tracing), and drawing samples from probability distributions in machine learning.

05

Bonus: darts in higher dimensions

The same game works in any number of dimensions. In d dimensions the square becomes a hypercube with sides of length 1, and the quarter circle becomes the corner piece of a d-dimensional ball of radius 1. A dart is inside if x₁² + x₂² + … + x_d² ≤ 1. Drag the slider to see what share of darts land inside.

Exact share of the cube inside the ball
—
Your 100,000 random darts
—

In 2 dimensions about 79% of darts hit. In 10 dimensions it's about 1 in 400, and in 20 dimensions about 1 in 40 million. Almost all of a high-dimensional cube lies outside the ball, out toward its corners.

This is the same high-dimensional strangeness that machine-learning books like Bishop's warn about. The 1/√N rule still holds, but if your target is a tiny sliver of the space, random darts almost never hit it. That's why serious sampling methods, such as Markov chain Monte Carlo, take guided random walks toward the regions that matter instead of throwing darts blindly.

06

Reverse Monte Carlo: running the question backwards

Ordinary ("forward") Monte Carlo starts from a model you already know, like the quarter circle, and uses randomness to compute a number from it, like π.

Reverse Monte Carlo (RMC) flips the direction. You start with measurements and use randomness to find a model that could have produced them. It was introduced by Robert McGreevy and László Pusztai in 1988 to work out the atomic structure of liquids and glasses. Diffraction experiments on those materials don't tell you where each atom is. They record something like a histogram of the distances between atoms. RMC builds a 3D arrangement of atoms whose distance histogram matches the experiment.

Monte Carlo
Known modelthe quarter circle
→
Random samplesthrow darts
→
A numberπ ≈ 3.14
Reverse Monte Carlo
Measured dataa histogram
→
Random tweakskeep the ones that fit better
→
A modelpositions that explain the data

The algorithm

  1. Start with a random guess: points scattered anywhere.
  2. Work out what the measurement would look like for this guess, and score the mismatch with the real measurement. The usual score is χ² = Σ (simulated − measured)² ÷ σ², summed over every histogram bin, where σ is how much error the measurement is allowed to have.
  3. Pick one point at random and nudge it a little.
  4. If χ² went down, keep the move. If it went up, usually undo it, but keep it anyway with probability e^(−Δχ²/2) so the search can climb out of dead ends. (This acceptance rule is borrowed from the Metropolis algorithm, with misfit playing the role of energy.)
  5. Repeat many thousands of times, until χ² stops falling and the remaining mismatch is comparable to the measurement's error σ.

Try it: recover a hidden shape from a histogram

A hidden shape inside the square has been "measured". The measurement (gold bars) only records how many points sit at each distance r from the bottom-left corner, much like a diffraction pattern only records distances. The algorithm never sees the shape itself. It starts with 600 randomly scattered points and has to find an arrangement that reproduces the histogram.

the algorithm's current pointshidden shape (when revealed)dashed arc: r = 1
Moves tried
0
Moves accepted
—
Misfit χ²
—
Points within r ≤ 1
—

Histogram of distances from the corner: measured (gold bars) vs the current guess (purple)

Misfit χ² as the moves pile up (log scale)

Things to try

  • Quarter disk: press Run, then tick "Reveal". The points drain out of the corner and fill the quarter disk, even though the algorithm was only ever shown a histogram of distances.
  • Ring: a harder target. Points have to leave both the middle and the corner.
  • Tolerance σ: a large σ means "the data is noisy, a rough match is fine", so stray points are tolerated. A small σ forces a much tighter fit.
  • Thin wedge: run it, then reveal the shape. The algorithm fills the whole quarter disk, not the wedge. That's not a bug: a thin wedge and a full quarter disk produce exactly the same distance histogram, so the data simply can't tell them apart.

That last experiment is the central caveat of Reverse Monte Carlo. It finds a model consistent with the data, not necessarily the true one. Whatever the measurement doesn't record (here, the angle), RMC fills in as randomly as it can. Real studies guard against this by combining several kinds of measurement and adding physical constraints, such as atoms not being allowed to overlap.

07

Side by side

Monte Carlo (forward)Reverse Monte Carlo
You start withA known model (the quarter circle)Measured data (a histogram)
What's randomWhere each sample landsWhich point moves, and where to
Each stepIndependent; every dart ignores the othersBuilds on the last; a chain of accepted and rejected moves
You end withA number (π ≈ 3.14)A model (positions consistent with the data)
Question answered"What does this model imply?""What model could explain this data?"
Main caveatAccuracy improves only like 1/√NThe answer isn't unique; it's one of many models that fit
Typical usesIntegrals, risk, particle physics, film renderingStructure of liquids, glasses and disordered materials

You'll sometimes hear "reverse" or "inverse" Monte Carlo used loosely for other things. This page uses the established meaning from materials science described above.