Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Monte Carlo Calculation of π

  1. Mar 17, 2017 #1
    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(*,*)'please seclect 12 numbers for your seed'

    READ(*,*) SEED

    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. jcsd
  3. Mar 17, 2017 #2

    BvU

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    Your counter_square is not what you think it is....
     
  4. Mar 17, 2017 #3

    Baluncore

    User Avatar
    Science Advisor

    A=4*(COUNTER_CIRCULE / N)
     
  5. Mar 17, 2017 #4
    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.
     
  6. Mar 17, 2017 #5

    Mark44

    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.
     
  7. Mar 25, 2017 #6
    Thanks for the advice. much appreciated
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted