Throwing Darts for Pi

Pi with a dartboard
Calculating pi with a dartboard. Red dots show the positions of darts as they land on our board.

The ancient Greeks defined the number \pi as the ratio of the circumference of a circle to its diameter. Since then we’ve discovered that \pi is incredibly important. It appears everywhere in physics, mathematics, and engineering. But how does one calculate it? \pi is an irrational number, so it’s impossible to calculate perfectly precisely. Nevertheless, it’s important to have an accurate approximation.

The Greeks originally calculated \pi by taking a piece of rope or twine of known length, bending it into the shape of a circle and comparing the diameter of the circle to the length of the twine. Since then, many many methods of calculating \pi have emerged. I’m going to talk about one such method. It’s not terribly accurate compared to other modern methods. However, it’s an important example of a much more general, powerful technique, called a Monte Carlo algorithm.

The above picture simulates a famous method of calculating \pi by throwing darts at a dartboard. We draw a square on our dartboard, each side of which is length 2r. Then we inscribe a circle of radius r in our square. The area of the square is then

    \[A_{square} = (2 r)\times (2r) = 4 r^2.\]

And the area of the circle was kindly calculated for us by the ancient Greeks:

    \[A_{circle} = \pi r^2.\]

So the ratio of the aria of the circle to the area of the square is

    \[\frac{A_{circle}}{A_{square}} = \frac{pi r^2}{4 r^2} = \frac{\pi}{4}.\]

Thus, if we can find this ratio, we can calculate \pi! One simple way to approximate these quantities is to simply throw darts at our dartboard. We count the number of darts that landed in the circle and compare it to the total number of darts that landed in the total area of the square board. The ratio of the number of darts in the circle to the number of darts in the square is approximately equal to the ratio of the respective areas…or, in other words, to \pi/4!

The accuracy isn’t very good, of course,Rf a Monte Carlo algorithm, which uses random number generation to calculate some physical quantity. Monte Carlo algorithms are used everywhere: in data analysis for detecting gravity waves, to make predictions about the quantum mechanics of materials, to numerically integrate difficult functions, and much, much more. For example, I use them in my research on quantum gravity.

If you’d like to play with the Python code I used for this simulation, I’ve put it online here.

Further Reading

If you’d like to learn more about probability theory, my good friend Michael Schmidt wrote some nice guest articles for me on it. Check them out:

(Michael has his own blog now, by the way. Take a look! The url is duality.io)

Related Links

I am by no means the first person to write about this algorithm. Here are some more resources.

  • Google’s Eve Anderson wrote a post about the algorithm here.
  • Here’s an applet that lets you calculate pi yourself. It has some history of the Monte Carlo algorithm too.
  • Ed from learntofish has a post with his implementation of the algorithm in Python.
  • +Ajay Ohri has a google+ post on this.
  • This article is a nice bit on the history of the Monte Carlo method.
  • I learned about it later, but +Vincent Knight wrote a very similar post to mine on Google+. You can find it here. He even designed an outreach event for high schoolers. You can find it here.