top of page
Writer's pictureRahul Jindal

Monte Carlo Approximation of Pi

Background

Imagine a circle being drawn on a grid with centre (0,0) and radius 1. Take the quarter of the circle located within the top right quadrant. Now if we were to randomly generate say a billion points with the x and y coordinates being greater than 0 but less than 1 the proportion of points expected to fall within the quarter circle should be approximately pi/4.

The Maths

The area of a circle is πr^2, and the area of the unit circle we have is simply π because the radius is 1. Now for the quarter circle we have above we simply divide that area by 4 which means the area is π ÷ 4. Therefore, if we were to take n random points within that square with sides of length 1, the proportion of points inside the circle divided by n should equal π/4. Simply multiplying both sides of the equation by 4 should allow us to find a value for π.


Let's begin to program this!

So this program shouldn't be too complicated to program, we will be using Python which has a build in library allowing us to generate the random numbers we need! We will also take advantage of the built in math library to calculate square roots.

For this program we will randomly generate 10 million points as this won't take too long to execute and will provide a fairly accurate final value.

  • First we will need to generate the random points within a for loop.

  • Now we can create a variable called 'inside' which holds the number of points that are located inside the circle.

  • The number of points within the circle can be calculated by finding the square root of the x coordinate squared + the y coordinate squared.

  • If this calculated value is less than or equal to 1 it means the point lies within or on the edge of the circle. .

  • From this we can choose whether or not to increment the variable 'inside'.

  • Once we have done this for 10 million points, we can then calculate π which will be 4 x (inside ÷ n), where n is the number of points we have used in this case we have chosen 10 million

  • Lastly we can print out π which should be approximately equal to the existing value of π

Here is my coded solution:

After Running the Code Several Times:

I always got a fairly accurate value for π, here is the actual value:

3.14159265359

Other Uses

The Monte Carlo Simulation can also be used to find out the area under curves, feel free to go research this for yourselves and let me know what cool things you find!


I hope you found this post interesting, please let me know if there's anything else you would me to look into and create a post about.

Thank you for reading :)

339 views2 comments

2 Comments


Joel Runevic
Joel Runevic
Oct 14, 2019

Very interesting post. I have an additional idea we could experiment with. Perhaps if we use the time module in python, we can see how long it takes to generate an accurate approximation of pi to an 'x' number of decimal places. We can then look at the relationship between the time taken, 't', and the number, 'x', of decimal places that our approximation agrees with to the real value of pi. I assume that it will be an exponential relationship of sort but it will be very cool to see what relationship it would specifically be. Just a thought.

Like

Rania Milki
Rania Milki
Oct 13, 2019

This is so cool

Like
bottom of page