What's Wrong with My Monte Carlo Calculation of π Using Fortran?

  • Context: Fortran 
  • Thread starter Thread starter Taylor_1989
  • Start date Start date
  • Tags Tags
    Calculation Monte carlo
Click For Summary

Discussion Overview

The discussion revolves around a participant's issues with a Monte Carlo simulation for estimating the value of π using Fortran. The focus is on identifying errors in the code and understanding the mathematical principles behind the calculations.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant expresses confusion over their code producing an approximation of 12 for π, indicating a potential error in the logic or calculations.
  • Another participant points out that the variable COUNTER_SQUARE does not represent what the original poster thinks it does.
  • A suggestion is made to change the calculation of A to use COUNTER_CIRCULE divided by N instead of COUNTER_SQUARE.
  • Concerns are raised about integer versus floating-point division in Fortran, highlighting that integer division could lead to incorrect results if COUNTER_CIRCULE is small.
  • One participant notes a spelling error in the term "circule," suggesting it should be "circle."

Areas of Agreement / Disagreement

Participants generally agree that the calculation of A needs to be corrected, but there is no consensus on the implications of integer division versus floating-point division, as well as the spelling issue raised.

Contextual Notes

Limitations include potential misunderstandings regarding variable definitions and the effects of integer versus floating-point arithmetic in Fortran, which are not fully resolved in the discussion.

Taylor_1989
Messages
400
Reaction score
14
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.

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
 
Technology news on Phys.org
Your counter_square is not what you think it is...
 
Taylor_1989 said:
A=4*(COUNTER_CIRCULE/COUNTER_SQUARE)
A=4*(COUNTER_CIRCULE / N)
 
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.
 
Baluncore said:
A=4*(COUNTER_CIRCULE / N)
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.
 
  • Like
Likes   Reactions: Taylor_1989 and BvU
Mark44 said:
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.
Thanks for the advice. much appreciated
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K