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

AI Thread Summary
The discussion centers on a coding issue related to estimating the value of Pi using a Monte Carlo method in Fortran. The user is experiencing unexpected results, specifically an approximation of 12, which is too high. Key points include the incorrect calculation of the variable A, which should be defined as A=4*(COUNTER_CIRCULE / N) instead of A=4*(COUNTER_CIRCULE / COUNTER_SQUARE). This correction addresses the issue of integer division versus floating-point division, which can lead to erroneous results if not handled properly. The conversation also highlights the importance of understanding type conversion in programming languages like Fortran. The user ultimately resolves the confusion by revisiting the mathematical concepts involved.
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 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
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Replies
2
Views
2K
Replies
1
Views
2K
Replies
1
Views
3K
Replies
1
Views
4K
Replies
3
Views
2K
Replies
2
Views
1K
Back
Top