Monte Carlo π Estimator

Throw thousands of random darts at a square, count how many land inside the circle, and out comes π. Then try the 18th-century version: dropping needles on a ruled floor.

Grades 7–12FreeNo sign-up

Method
Target

Convergence: estimate after n trials

running estimateπ = 3.14159…95% band: π ± 1.96·σ/√n

How can random darts know about π?

Draw a square with side 1 and, inside it, a quarter circle of radius 1 centred on one corner. The square has area 1. The quarter circle has area ¼ · π · 1² = π/4. Now throw darts so that every point of the square is equally likely to be hit. The chance that a dart lands inside the quarter circle is simply the fraction of the square it covers:

P(inside) = (area of quarter circle) ÷ (area of square) = π/4 ≈ 0.7854

After many darts, the observed fraction inside will be close to π/4 — that is the law of large numbers, the same idea as in the coin and dice simulator. Multiply that fraction by 4 and you have an estimate of π. The Full circle option uses a circle of radius 1 inside a 2 × 2 square: areas π and 4, so the ratio is again π/4. (Area formulas are in the circle calculator.)

A dart at (x, y) is inside when x² + y² ≤ 1 — the distance formula — so the simulation never uses π to decide. Methods that get answers from random sampling are called Monte Carlo methods, after the casino.

Example: out of 10,000 darts, 7,871 land inside. The estimate is 4 × 7,871 ÷ 10,000 = 3.1484, an error of about 0.007.

How to use the lab

Why the error shrinks like 1/√n

Each dart is a yes/no trial with success probability p = π/4. The fraction of hits in n darts has standard deviation √(p(1 − p)/n), so the estimate 4 × fraction has standard deviation

σ/√n, where σ = 4·√(p(1 − p)) = √(π(4 − π)) ≈ 1.64

The shaded band on the chart is π ± 1.96·σ/√n; about 95% of the time the estimate lies inside it. The important part is the √n. To make the error 10 times smaller you need 100 times as many darts:

Darts n10010,0001,000,000
Typical error 1.64/√n0.160.0160.0016
Correct digits (roughly)33.13.14

That is slow — a million darts for three digits — so nobody computes π this way. The value of Monte Carlo is that the same 1/√n rule holds for problems far too messy for a formula, such as areas of irregular shapes and averages over many dimensions.

Buffon’s needle

In 1777 the Comte de Buffon asked: if a needle of length ℓ is dropped on a floor ruled with parallel lines a distance d apart (with ℓ ≤ d), what is the probability that it crosses a line? A needle making angle θ with the lines spans a height ℓ·|sin θ|, so it crosses with probability ℓ·|sin θ|/d. Averaging |sin θ| over all directions gives 2/π, hence

P(cross) = 2ℓ / (π d), so π ≈ 2ℓ·n / (d · crossings)

With ℓ = d the rule is simply π ≈ 2 × needles ÷ crossings. The error again shrinks like 1/√n, but with a larger constant (about 2.37 when ℓ = d), so needles converge a little more slowly than darts. Shorter needles cross less often and do worse still — try the slider.

Experiments to try

  1. Reset, then add 100 darts and write down the estimate. Repeat ten times. What are the smallest and largest values? Now do the same with 10,000 darts each time.
  2. How many darts do you need before the estimate reliably starts with 3.1? With 3.14? Compare with the table above.
  3. Does the full circle give better estimates than the quarter circle with the same number of darts? Explain using the area ratio.
  4. In Buffon’s experiment, set the needle to half the line spacing. Predict the fraction of needles that cross, then test it.
  5. Watch the running estimate on the chart. Does it ever leave the shaded band? About how often should it?

For another experiment where randomness produces a predictable pattern, try the Galton board.

Frequently asked questions

How does the Monte Carlo method estimate pi?

Random points are scattered uniformly over a square that contains a quarter circle (or a full circle). The fraction of points landing inside the circle approximates the ratio of the areas, which is pi/4, so multiplying that fraction by 4 gives an estimate of pi.

How many random points are needed to get pi to 3.14?

The typical error is about 1.64 divided by the square root of the number of points. To be reliably within 0.005 of pi you need on the order of a few hundred thousand to a million points. Each extra correct digit costs about 100 times more points.

Why does the error decrease like 1 over the square root of n?

Each point is an independent yes/no trial. The standard deviation of the proportion of successes in n trials is the square root of p(1 − p)/n, so the spread of the estimate is proportional to 1/√n. Quadrupling the number of points only halves the error.

What is Buffon’s needle problem?

It asks for the probability that a needle dropped at random on a floor with parallel lines crosses a line. For a needle of length l and line spacing d (with l no longer than d) the probability is 2l/(pi·d), which can be turned around to estimate pi from the observed crossing rate.

Is Monte Carlo a good way to calculate pi?

No — it is far too slow. Formulas based on series give millions of digits quickly. The dart experiment is valuable because it shows how random sampling can estimate areas and probabilities that have no simple formula.

Why do I get a different answer every time?

The points are random, so each run is a new sample. The estimates scatter around pi, and the scatter gets smaller as the number of points grows. The simulator uses your browser’s pseudo-random number generator.