Monte Carlo Pi

Using random numbers, it’s possible to approximate $\pi$.

Here’s the strategy: Draw a unit circle (radius of $1$), and pay attention to the portion in the top right quadrant. The area of the square that surrounds this quarter-circle is $1$ because each of the sides is $1$ (see the image below). The area of the circle is $\pi r^2=\pi\times 1=\pi$. So, the area of the quarter-circle is $\frac{\pi}{4}$. The ratio of the area between the quarter-circle and the square is then $\frac{\pi}{4}:1=\frac{\pi}{4}$. This means if we were to hang these shapes on a wall and throw darts at them randomly, $\frac{\pi}{4}\approx75.5\%$ of them would be within the quarter-circle. So once we finish throwing a bunch of darts, all we need to do is multiply the quarter-circle:square ratio that we observe by $4$ to approximate $\pi$.




2points/sec


Points tested: 0
Pi is approximately equal to: 0

Techniques like this one that use random sampling to solve problems are called “Monte Carlo methods”. Why the randomness? Why not just throw darts procedurally down each row and column? That would work well for this problem because we wouldn’t have to test too many points. A $400\times 400$ image has $160,000$ pixels, which would be easy to run through on a modern computer. However, for some problems, running through every possibility would take far too much time. Monte Carlo methods are hugely beneficial in these cases because they allow you to get a good sense of what the sample-space looks like without actually sampling every single point.

Fork me on GitHub