Monte Carlo approximation of pi

As an example, we will implement a canonical, embarrassingly parallel program--the Monte Carlo approximation of pi. Imagine that we have a square of size 2 units; its area will be 4 units. Now, we inscribe a circle of 1 unit radius in this square; the area of the circle will be pi * r^2. By substituting the value of r in the previous equation, we get that the numerical value for the area of the circle is pi * (1)^2 = pi. You can refer to the following figure for a graphical representation.

If we shoot a lot of random points on this figure, some points will fall into the circle, which we'll call hits, while the remaining points, misses, will be outside the circle. The area of the circle will be proportional ...

Get Advanced Python Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.