PIEXPT.F95
PROGRAM PIEXPT ! ! Purpose: To obtain an approximate value of pi. ! IMPLICIT NONE INTEGER::I INTEGER::N,NCIRCLE REAL:: X,Y REAL:: PIAPPR ! WRITE(*,*)'How many points ?' READ(*,*)N ! ! Initialize NCIRCLE and draw N random points. NCIRCLE=0 DO I=1,N CALL RANDOM_NUMBER(X) CALL RANDOM_NUMBER(Y) ! Redefine X and Y so as to have values distributed between -1 and +1, ! rather than between 0 and 1 (as returned by RANDOM_NUMBER). X = 2.*(X-0.5) Y = 2.*(Y-0.5) ! ! Method: the area of a circle circumscribed in the square is exactly pi, ! since the radius is 1. The area of the square is exactly 4. Since the random ! points are uniformly distributed, the ratio of the area of the circle ! to the area of the square is approximately equal to the ratio of the number ! of random points falling inside the circle to the quarter of the total ! number of random points. ! IF(X**2+Y**2 <= 1)NCIRCLE=NCIRCLE+1 END DO ! ! Approximate value of pi PIAPPR=4.*REAL(NCIRCLE)/REAL(N) WRITE(*,*)'Approximate value: ',PIAPPR WRITE(*,*)'Exact value: ',4.*ATAN(1.) STOP END PROGRAM PIEXPT