[FORTRAN 90] Pi by Monte Carlo Simulation : How to print results out as a function of N?

Language

Hello! I've attempted to write a program to calculate N using random numbers, and I need to follow these exact steps:

1.Create a randomly placed point on a 2D surface within the range [−1, 1] for x and y, using call random_number(x).

2.calculate how far away the point is from the origin, i'll need to do both of these steps for N points.

3.for each N value work out the total amount of points that are less than 1 away from origin. Calculate pi with A=4pir2

4.use a do loop to calculate pi as a function of N and output it to a data file. then plot it in a graphing package.

Here's my code:

program pi 
implicit none

integer :: count, n, i
real :: r, x, y
count = 0

CALL RANDOM_SEED
DO i = 1, n
 CALL RANDOM_NUMBER(x)
 CALL RANDOM_NUMBER(y)
 IF (x*x + y*Y <1.0) count = count + 1
END DO
r = 4 * REAL(count)/n
print *, r
end program pi

This prints 3.14902186 to the terminal. However, I want it to print values of pi as a function of N (where N is N points), how can I change my code to do this?

Thanks !

Posted Aug 17, 2017, 7:55 PM UTC · edited

14
Loading comments…