Average theoritical value of π using Monte Carlo method

  The Monte Carlo method provides an approximate result due to probabilities used in the related theoretical calculus. In this technique, we start finding a model leading to the expected result, generate random numbers, evaluate the model to give the result, make iterations and comment the result.
In this example, we want to find the value of π number. The area of the quarter of the square "As" is equal to 1 x 1 /4 = 1/4. The area of the quarter of the circle "Ac" is equal to π x 1 x1 /4 = π/4. When we generate random numbers, a total number Nc had reached the quarter of the quarter of the circle; and a total number Ns had reached the quarter of the square. We have:
Ac/As = Nc/Ns → π = Nc/Ns
The more we generate numbers, the more the result is precise.
Source code


Average value of π using Monte Carlo method and PHP scripts:

  Enter an integer: