Over the years, mathematicians have dedicated tremendous amounts of research to conceiving of new and faster methods of approximating Pi. Hundreds of books have been written on this remarkable constant, and on disparate attempts to get another thousand digits. The current, bleeding-edge algorithms are remarkably fast, but also, quite frankly, often mind-boggling. When looking at all of these crazy algorithms, thereís often the risk of losing sight of the forest for the trees. Sometimes, itís fun to look at a method that you can understand and comprehend intuitively with nothing more than middle-school math, just to get a new, deeper perspective on Pi and what it really means. Thatís where the Monte Carlo method of approximating Pi, probably one of my favorites, comes in. A Monte Carlo method is simply any method that uses repeated random simulations to calculate probabilities. Basically, instead of using Ďrealí/theoretical statisitcs to calculate the probability of heads on a fair coin, just simulate flipping it a million times and record what percentage is heads/tails. While it may not be as accurate, and is potentially orders of magnitude slower, the Monte Carlo method has the advantage of being extremely easy to comprehend conceptually, and usually also to implement.
Where does Pi come in? Well, first some background ( is a radius, is a side length):
Area of circle : PI*r2 & Area of Square : s2
Now consider a Circle inscribed in a square, with radius , like so:
Since the center point of the circle is the same as that of the square, it follows that the side length of the square () would be equal to twice the radius:
Therefore, by substituting for , we can arrive at the following to represent the area of the square using the radius of the inscribed circle:
Area of square=s2 = (2r)2=4r2
Now we can return to our Monte Carlo method. I like to think of this particular method in terms of a dart board because, well, the inscribed circle looks just like a target. Imagine you have an infinite supply of darts, and you throw them at random locations on the dart board. You donít try to aim inside or outside the circle (or maybe you do if, like me, youíre bad enough for it not to matter), and none of your darts miss the board entirely. Now, if you kept this up for an infinitely long time, the ratio between the darts in the circle and the darts in the square should theoretically become equal to the ratio between the area of the circle and the area of the square. So, considering that none of our darts land outside the square, we can say:
4 * Darts in circle/Darts thrown = Area of circle / area of square =4* PI / 4 =PI
Obviously, as we throw more darts, the ratio becomes closer to the theoretical probability and our approximation gets more accurate. Now lets try implementing it! Feel free to skip to the bottom if youíre not interested in the code, and just want to see the simulation.