B prev next
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

<< MC APPLICATIONS >>

Computing Pi

Let's look at the unit circle (radius=1) within a square with sides equal to 2 (see figure). Now if we pick a random point (x,y) were both x and y are between [-1,1], the probability of that this random point lies inside the unit circle is given as the proportion between the area of the unit circle and the square:

If we pick a random point N times and M of those times the point lies inside the unit circle, the probability of that a random point lies inside the unit circle is given as:

where the dot indicates that this is a discreet distribution (because M and N are integers). But if N becomes very large (theoretically infinite), the two probabilities will become equal and we can write:

Output:

       N             PI         Error
 -------------   ---------    ----------
            10    3.200000      0.058407
           100    3.120000      0.021593
         1,000    3.156000      0.014407
        10,000    3.166800      0.025207
       100,000    3.138880      0.002713
     1,000,000    3.141920      0.000327
    10,000,000    3.140899      0.000694
   100,000,000    3.141710      0.000117
 1,000,000,000    3.141604      0.000012

The computer implementations : pi.f90 | pi.c | pi.cpp | pi.exe | pi.html