Approximating Pi (Monte Carlo Method)

March 14th, 2015

In honor of P​i Day,​ I’ve decided to share a cool trick for approximating the number pi using the Monte Carlo Method. I’ve built a simple HTML5/JavaScript applet to demonstrate the Monte Carlo simulation further down on this page. Feel free to copy and paste the code into a text editor to learn more about how the algorithm works.


A Bit About Pi

Pi ​(approximately 3.14159) is an irrational number (can’t be represented as a fraction) with infinite digits. In the real world, pi (represented by the Greek letter π) is the ratio between the circumference and diameter of any circle. It’s philosophically interesting because it’s a non-repeating decimal, meaning that the digit combinations are random (without an infinitely repeating pattern). Because of this randomness, it’s possible that your name in ASCII could be represented somewhere in this long digit string. Some people speculate that every written and unwritten book in the world could be represented somewhere down the line in Pi’s vast array of digits. Although it’s a beautiful thought, it’s important to remember that just because something is possible doesn’t mean that it must happen.

Approximating Pi

We understand that pi is a ratio between the circumference and diameter of any circle. In the real world, it’s easy to measure the circumference of a circle using a tape ruler. In the computing world, however, getting the precise length of an arc is virtually impossible since every number in a computer is treated as discrete rather than continuous. The distance between two points is measured as a line, not a curve (see the distance formula). How do we get around the problem of measuring curves? We approximate!


First, imagine a square with a circle inscribed inside it. Now divide this square up into four quadrants. The side length of each quadrant is the radius of the circle. To save time, we will only use one quadrant in our simulation. For some duration of time (in this example, thirty seconds), we will place dots randomly in this quadrant. Then we will approximate pi by dividing the number of dots in the circle by the number of dots total (in both the circle and the square), and then, multiplying that quotient by four.

Monte Carlo Pi Approximation

Click the button to view a thirty-second animation.

Your browser does not support the HTML5 canvas tag.

In the simulation above, we can tell that the orange dots resemble a quarter of a circle while the blue dots resemble the space outside outside of that circle. How do we know whether to make the dots orange or blue? If the distance between the center of the circle and any given point has a distance less than or equal to the circle’s radius, we say that the point is within the circle (and color it orange). Otherwise, we say that the dot is outside of the circle (and color it blue).


Code for the Simulation

Below is the code I wrote for the Monte Carlo simulation on this page. It’s in HTML5 and JavaScript. To see this demonstration in action, simply copy and paste the code below into a text editor and save as approxPi.html.​ Next, open the file with a web browser to play with the simulation. Happy Pi Day!

Things to Think About

  • Could you use the digits of pi to make a good random number generator?

  • What’s possible may not be actual. Is the reverse true?

  • What’s the largest number of pi digits computed today?


Categories: mathphilosophytechnologytutorials