Hello everyone, I'm trying to calculate pi by following an idea on Wikipedia
In practice I generate some random points (x,y) in a square (side=1) and counting how many points are in the square and how many are in the circle (radius=1) remembering that I'm considering a quarter of circle, not a full circle.
The problem is that I set an error threshold of 10^(-8) but the program stops after 120217 steps, with an error of 7*10^(-6).
How can I improve my program?
Thank you!
P.S. sorry for my bad English, I'm not mother tongue.
In practice I generate some random points (x,y) in a square (side=1) and counting how many points are in the square and how many are in the circle (radius=1) remembering that I'm considering a quarter of circle, not a full circle.
The problem is that I set an error threshold of 10^(-8) but the program stops after 120217 steps, with an error of 7*10^(-6).
How can I improve my program?
Thank you!
P.S. sorry for my bad English, I'm not mother tongue.
Code:
program pi_montecarlo
implicit none
double precision::x,y,error,pi_calculated
integer::i,n,square,circle
double precision, parameter::pi=3.141592653589793238462643383279
square=0
circle=0
error=1.
n=0
do while (abs(error)>1e-10)
x=ran()
y=ran()
if (sqrt(x**2.+y**2.)<=1.) then
circle=circle+1
square=square+1
else
square=square+1
end if
pi_calculated=4.*(float(circle)/float(square))
error=(pi_calculated-pi)
write(*,*)"steps=",n,"pi=",pi_calculated,"error=",error
n=n+1
end do
end program