Hi,I would like to point to the fact that this particular method works especially well when you make 14 pairs of random numbers. It works very well if you generate 452 numbers.
Using 14 pairs of random cooridante pairs you can get:3.1428571428571
Using 452 you can get as close to Pi as 3.141592920354
Probably you can work out why. ;)
I used Lua to generate these numbers, and the script is:
x = 0
y=14
for i = 1, y
do
a=math.random()
b=math.random()
if (a^2+b^2<1 ) then x=x+1 end
end
print(4*x/y)
I think you can get a generally better approximation using something like this:
S = 0
n=100
for i = 1, n
do
x=math.random()
y=(1-x^2)^0.5
S=S+y
end
T=4*S/n
print(T)
-- (August 12, 2005) on Calculation of Pi Using the Monte Carlo Method