Calculate Pi using Monte Carlo method
Please ⭐ if you like it!
- Use a square of arbitrary side length and inscribe a circle inside it.
- Put random points on the square using a uniform distribution.
- Count the number of random points falling inside the circle.
- Calculate the approximate value of pi using,
4 * points inside the circle / total number of points
.
Why 4 * points inside the circle / total number of points
?
Let's consider,
Radius of circle, r
Side of square, 2r
Number of points inside the circle, C
Total number of points, S
Thus,
Area of circle = πr²
Area of square = 4r²
For large enough number of random points, we can consider that the ratio of areas of circle to square is equal to the ratio of points inside the circle to the total number of points, ie, πr²/4r² = C/S
.
Which can be simplified to, π = 4*C/S