Estimating Pi using the Monte Carlo Method

pi-3-14159One method to estimate the value of π (3.141592…) is by using a Monte Carlo method. This methods consists of drawing on a canvas a square with an inner circle. We then generate a large number of random points within the square and count how many fall in the enclosed circle.
estimating-pi-monte-carlo-method

The area of the circle is πr2,
The area of the square is width2 = (2r)2 = 4r2.

If we divide the area of the circle, by the area of the square we get π/4.

The same ratio can be used between the number of points within the square and the number of points within the circle.

Hence we can use the following formula to estimate Pi:

π ≈ 4 x (number of points in the circle / total number of points)

Python Turtle Simulation


Run the code below to estimate Pi using the Monte Carlo Method.

Note: You may reach a better estimate of Pi by:

  • Increasing the radius of the circle. (e.g. radius = 1000) as it will give you a circle with a “higher resolution”.
  • Increasing the number of dots. (e.g. total = 5000)

Extension


You can find out more about the Monte Carlo method and its applications.
unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 4.8 / 5. Vote count: 12

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: ,