Rolando Andrade
Back to projects

Built in 2018

PiDay 2018

PI from Random Darts

PiDay 2018 preview

Estimation of the PI number by throwing random points at a circle and counting how many land inside.

Draw a circle inside a square. Now throw darts at the square without aiming. Some darts land inside the circle. Some land in the corners. Count them, multiply by 4, and you get pi.

It sounds like a magic trick, but it is plain geometry. You do not need a single formula with infinite terms. You only need randomness and a lot of patience. A computer has plenty of both.

This was my project for Pi Day 2018. Pi Day is March 14, because the date reads 3.14 in the American format. It calculates pi with one of the simplest ideas there is, and you can watch it happen on screen.

A dartboard on a canvas

The page draws a 200 by 200 pixel square on an HTML Canvas, the browser’s drawing surface. Inside the square sits a circle with a radius of 100 pixels. The circle touches all four sides of the square, like a dartboard in a box.

Then the darts start to fall. Each dart is one tiny dot, one pixel wide. The dots appear fast, and the square slowly fills with a cloud of light blue points. When the last dart lands, the page writes the estimate of pi in white, right in the center of the board.

Pi lab.rolandoandrade.me

Why the ratio is pi

The trick comes from comparing two areas.

  1. The circle has a radius of 100. Its area is pi times 100 times 100.
  2. The square has sides of 200. Its area is 200 times 200, which is four times 100 times 100.
  3. Divide the circle area by the square area. The 100 times 100 cancels out, and you get pi divided by 4.

So if darts land at random all over the square, about pi over 4 of them land inside the circle. That is roughly 78.5 percent. Flip the equation around, and pi is 4 times the share of darts inside the circle.

This family of methods has a name: the Monte Carlo method. It uses random samples to estimate a number that is hard to calculate directly. Scientists use the same idea to study weather, finance and particle physics. Here it finds pi.

Inside or outside

Each dart needs two random numbers, one for x and one for y. JavaScript’s Math.random() gives a number between 0 and 1. The code multiplies it by 200, so the dart can land anywhere on the square.

To know if a dart hit the circle, the code measures its distance to the center at (100, 100). It uses the Pythagorean theorem: square the horizontal gap, square the vertical gap, add them, and take the square root. If that distance is 100 or less, the dart is inside, and a counter goes up by one.

That is the whole algorithm. Two random numbers, one distance, one comparison. Repeat.

One hundred thousand darts

The program throws 100,000 darts in total. It does not throw them all at once. A timer fires every millisecond, and each tick throws 50 darts. This keeps the browser free to draw, so you see the dots appear instead of a frozen page.

When the counter reaches 100,000, the timer stops. The page computes 4 times the darts inside, divided by the total. The result appears in the center of the circle and also below the canvas.

An older version still lives in the code as a comment. It threw 1,500,000 darts in a single loop and printed only the final number. It was more precise, but you could not watch it work. The timer version trades some precision for a show.

Close, but never exact

Randomness has a cost. Every time you load the page, you get a slightly different answer. The darts fall in new places, so the count changes a little.

More darts give a better answer, but slowly. To get one more correct digit, you need about 100 times more darts. With 100,000 darts, the estimate usually lands within about 0.01 of the real value. That is good enough to see 3.14, and not much more.

Compare that with my Pi Day 2020 project, which uses the Leibniz series. That series is slow too, but it always gives the same numbers in the same order. The Monte Carlo method never repeats itself.

Small code, big idea

The whole project is one HTML file. There is no library, no build step and no framework. The canvas draws a rectangle, a circle and many tiny dots. A few lines of JavaScript do the rest.

This project shows that pi is not only about circles on paper. It also hides inside chance. Throw enough random darts, and pi shows up on its own.

The next year I tried a stranger idea: counting collisions between two balls. You can read about it in Pi Day 2019.

Interested in working together? Check out my services and pricing to see how we can collaborate on your next project. Let's Build