This program selects random points in the rectangle (0,0) - (1,1). A circle with radius 0.5 centered
in that rectangle has area Pi * (0.5)^2 so the number of points that lie within that circle should
be approximately:
[points in circle] = [total points] * [area circle] / [area rectangle]
= [total points] * [Pi * (0.5)^2] / [1]
= [total points] * [Pi / 4]
Solving for Pi:
Pi = 4 * [points in circle] / [total points]
The program makes the calculation a little easier by noting that a point (X, Y) is in the circle
if:
Sqr(X^2 + Y^2) < R^2
where R is the radius of the circle. Replacing R with 1 and squaring both sides gives:
X^2 + Y^2 < 1
|