Kolton Ndoj

Monte Carlo: finding π by throwing random darts

In weather forecasts, game AI, movie lighting and nuclear-physics simulations, which all answer hard questions with lots of random tries

What problem does it solve?

Can you calculate π using nothing but random numbers?

The big idea

Draw a quarter circle of radius 1 inside a 1×1 square. Its area is π/4, so a random dart landing in the square hits the quarter circle with probability exactly π/4. Throw enough darts, count the hits, and π falls out of pure chance. The deep idea: when a problem is too complicated to solve exactly, simulate it randomly many times and let averages do the work.

How it works, step by step

  1. 1. Pick two random numbers x and y between 0 and 1: that's a dart in the square.
  2. 2. If x² + y² ≤ 1, the dart is inside the quarter circle.
  3. 3. Repeat thousands of times, counting hits.
  4. 4. Estimate π ≈ 4 × hits ÷ total darts.

The key formula

π≈4⋅hitsN\pi \approx 4\cdot\frac{\text{hits}}{N}
NN
the total number of random darts
hits\text{hits}
darts with x² + y² ≤ 1, inside the quarter circle
44
because the quarter circle covers π/4 of the square

How fast is it?

Slow but simple: the typical error shrinks like 1/√N, so each extra correct digit needs about 100 times more darts. Its superpower is that the same trick works in 100 dimensions, where exact methods drown.

Graph for Monte Carlo: finding π by throwing random darts 10100000.60
Typical size of the error in the π estimate after t darts (about 1.64/√t). 100 times more darts gives only 10 times less error. (drawn with the default slider values — move the sliders live)

Try it yourself

Fun fact

The name comes from the Monte Carlo casino in Monaco. Stanisław Ulam got the idea in 1946 while estimating his chances of winning a game of solitaire, and used it with John von Neumann to simulate neutrons for nuclear physics.

Related

Play with the live demo →

Theory · Beginner-friendly