Monte Carlo estimation of PI


Author Message
GerryL
Regular Member

Joined: 24/01/2019
Location: Australia
Posts: 45
Posted: 02:08am 08 Sep 2020      

Hi Nim,
I had a similar program on the ARM7 (see cut down version below with all the display stuff removed) and what interested me, apart from how fast it ran, was why its accuracy was no better than a couple of decimal places no matter how long I ran the program. There is probably a sound reason for this but I never figured it out.  

DIM as FLOAT  X =0
DIM as FLOAT  Y =0
DIM as FLOAT fInsideCircle = 0
DIM as FLOAT  fTotal = 0
DIM as FLOAT  f_Pi
DIM as FLOAT fError

Do
X = RND()
Y = RND()
if sqr(X^2 + Y^2) <= 1.0 then fInsideCircle = fInsideCircle + 1
fTotal = fTotal + 1
f_Pi = 4.0 * (fInsideCircle/ fTotal)
fError = PI - f_Pi
Loop while inkey$ = ""


Do you get PI better than a couple of decimal places?

Gerry