One 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.
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.
Solution...
The solution for this challenge is available to full members!Find out how to become a member:
➤ Members' Area