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:
Python Turtle Simulation
Run the code below to estimate Pi using the Monte Carlo Method.
You can find out more about the Monte Carlo method and its applications.