This is an exercise to estimate the values of π using Monte Carlo Algorithm
This method tries to find the value of π by estimating if a randomly chosen point lies inside a circle. We choose the radius of the circle to be 1000 units. The co-ordinates of the choosen points would range from (0, 0) to (1000, 1000).
Mathematically it boils down to
Area of quarter circle / area of square enclosing the quarter circle = number of points inside quarter circle / number of points inside square
⇒ [(3/4) * πr² / r² = k
where k = number of points inside quarter circle / number of points inside square (k is found by experiment)
⇒ (3/4) π = k
⇒ π = k * (4/3)
Now if we have n
number of points, the value of k
is found by the following logic
The distance of a point from origin is
d = sqrt(x'² + y'²)
x'
and y'
denote randomly generated numbers
The point lies inside the circle if
d < r
So k
turns out to be the count of points for which
d < r
This algorithm can be used to test the randomness of the random number generator methods / function available in different languages.
Select a sample size and run the simulation for a thousand times. Increase the sample size and observe how the error in π reduces.
The sample size and the average, best and worst errors are tabulated.