Signal and Sensation

One More Digit

Throwing darts at a circle finds π in seconds, then spends the rest of eternity finding the next decimal place.

Open fullscreen →

What it is

Darts land uniformly in a square. The fraction inside the quarter circle is π/4, so counting hits estimates π. The green trace on the right is the actual error, the amber line is the error the theory predicts. Tap to start again.

Watch the first two digits appear almost immediately, then watch how long the third takes.

How it works

Monte Carlo error falls as one over the square root of the sample count. For this particular estimator the hit probability is π/4, so the standard error works out at almost exactly 1.64/√N, which is the amber line, drawn from arithmetic rather than fitted to the run.

One over root N sounds gentle. It isn’t. To halve the error you need four times the samples. To gain a decimal place you need a hundred times.

decimal places darts needed
1 1,100
2 110,000
3 11 million
4 1.1 billion
6 11 trillion

At a million darts a second, six decimal places takes four months. Archimedes got that far with polygons and a straight edge.

What surprised me

The test I expected to be routine was the interesting one.

Checking that error falls as 1/√N is easy but circular, because it tests the formula against itself. So I added one that runs the actual dice: eight seeded runs of forty thousand darts each, asserting that at most one exceeds three times the predicted error.

That’s a genuinely different kind of test. It can fail by chance. Three sigma should be exceeded about three times in a thousand, so with eight runs the expected count is 0.024 and my threshold of “at most one” is generous, but it’s still a probabilistic assertion sitting in a suite that’s otherwise deterministic.

That’s the right shape for the thing being tested. A Monte Carlo estimator has no correct answer, only a distribution, and a test demanding a specific value would be testing the seed rather than the method. It took me a moment to be comfortable writing an assertion that’s only almost certainly true.

What I would do next

The same estimate with stratified sampling, which improves the exponent rather than the constant. That’s the only way to actually win.