# Monte Carlo Calculation of π

1. Mar 17, 2017

### Taylor_1989

Hi guys, I am having trouble seeing where I have actually gone wrong with my code. If I run the code I am getting approximation of 12 as my lowest is way to big. But I am really struggling to find where I have gone wrong. Any advice would be appreciated.

Code (Fortran):

PROGRAM assign_10_1

IMPLICIT NONE

INTEGER, DIMENSION (1:12) :: SEED

REAL:: A,X,Y

INTEGER:: I, N, COUNTER_CIRCULE, COUNTER_SQUARE

COUNTER_CIRCULE=0

COUNTER_SQUARE=0

N=100000

WRITE(*,*) SEED

CALL RANDOM_SEED(PUT=SEED)

DO I=1,N
CALL RANDOM_NUMBER(X)
CALL RANDOM_NUMBER(Y)
WRITE(*,*) X,Y

IF (X**2+Y**2 < 1) THEN
COUNTER_CIRCULE=COUNTER_CIRCULE+1
ELSE
COUNTER_SQUARE=COUNTER_SQUARE+1
END IF

END DO

A=4*(COUNTER_CIRCULE/COUNTER_SQUARE)

WRITE(*,*)'ESTIMATION FOR VALUE OF PI', A

END PROGRAM

2. Mar 17, 2017

### BvU

Your counter_square is not what you think it is....

3. Mar 17, 2017

### Baluncore

A=4*(COUNTER_CIRCULE / N)

4. Mar 17, 2017

### Taylor_1989

Ok thanks guys. I got it. I just went back of the math with a bit of paper and it clicked in what you are saying. Thanks once again.

5. Mar 17, 2017

### Staff: Mentor

Not mentioned is the fact that if COUNTER_CIRCULE is 1000, then A will be assigned the value 0. In Fortran, C, C++, and many other programming languages, there are two types of division: integer division and floating point division. So for example, 2/5 is 0 while all three of the following expressions evaluate to 0.4 or something close to it.
• 2.0 / 5.0
• 2 / 5.0
• 2.0 / 5
See http://www.oc.nps.edu/~bird/oc3030_online/fortran/basics/basics.html, in Type Conversion and Mixed-Mode Arithmetic.

BTW there is no such word as "circule" in English.

6. Mar 25, 2017

### Taylor_1989

Thanks for the advice. much appreciated