Monte Carlo Approximation of Pi
Given a unit circle (r=1) inscribed inside a square of side length 2, we can calculate the areas of each shape as follows:
\[A_{square} = (2r)^2 = 4 A_{circle} = \pi (r)^2 = \pi\]
Using a Monte Carlo simulation, we can then assess how many random points land within the circle compared to how many points land outside of the circle.
Proportion of points inside circle = \(\frac{A_{circle}}{A_{square}} = \frac{\pi}{4}\)
Therefore, we approximate \(\pi\) as 4*proportion.
\[A_{square} = (2r)^2 = 4 A_{circle} = \pi (r)^2 = \pi\]
Using a Monte Carlo simulation, we can then assess how many random points land within the circle compared to how many points land outside of the circle.
Proportion of points inside circle = \(\frac{A_{circle}}{A_{square}} = \frac{\pi}{4}\)
Therefore, we approximate \(\pi\) as 4*proportion.
The method is of course fairly straightforward, so I won't belabor you with the details here. The majority of the code is actually for pygame's visualization rather than the actual approximation.
Classic Fizzbuzz
Below is a screenshot of the pygame window. We see that the approximation is already quite good with relatively few points.