Random PI

Here is a classic example of generating π from a finite sequence of random numbers. Consider a unit circle (r = 1) inscribed inside a square (sides of L = 2). The area of the circle is π (π r2 and the area of the square is 4 (L2). If the square is populated with random points (x,y), the ratio of points falling inside the circle to the total number of points will be the area ratio π/4. It would seem that more random points would generate a better approximation of π. That is not the case since π is irrational - it can not be calculated as the ratio of two whole numbers. But there still exist close approximations, most famous are 22/7 (0.04% error) and 355/113 (0.0000085% error), both of which have suitable accuracy for many calculations. Due to the random nature of the calculations these two results might not be encountered.

Three different random number generators are available with similar results - update rate about 60/s.

MATHS

Consider a series of random numbers, xN and yN, such that
\begin{align} (-1 \leq {x_N} \leq 1) \end{align} and \begin{align} (-1 \leq {y_N} \leq 1) \end{align} The point will fall within the circle if \begin{align} {x_N}^2 + {y_N}^2 \leq 1 \end{align} let N1 be the number of points falling inside the circle, then by ratio of the area of the circle to the total area \begin{align} \frac{\pi}{4} \approx \frac{N_1}{N} \end{align} then \begin{align} {\pi} \approx \frac{4N_1}{N} \end{align} The calculations keep track of the best approximation.