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

In summary, the programmer is struggling to understand why their code is not producing the correct results. They are unsure of where they have gone wrong and would appreciate any advice offered.
  • #1
Taylor_1989
402
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
  • #2
Your counter_square is not what you think it is...
 
  • #3
Taylor_1989 said:
A=4*(COUNTER_CIRCULE/COUNTER_SQUARE)
A=4*(COUNTER_CIRCULE / N)
 
  • #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.
 
  • #5
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
  • #6
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
 

1. What is Monte Carlo calculation of π?

The Monte Carlo method is a computational technique that uses random sampling to solve problems in various fields, including mathematics. In the context of calculating π, it involves generating a large number of random points within a square and determining the ratio of points that fall within a quarter circle inscribed within the square. This ratio can be used to estimate the value of π.

2. How accurate is the Monte Carlo calculation of π?

The accuracy of the Monte Carlo method depends on the number of random points generated. The more points that are used, the closer the estimated value of π will be to the actual value. With a large enough number of points, the accuracy can be very high.

3. What are the advantages of using Monte Carlo calculation of π?

One advantage of using the Monte Carlo method is that it is relatively easy to implement and can be used to solve complex problems that may not have a straightforward analytical solution. Additionally, it can be applied to a wide range of problems, making it a versatile tool for scientific research.

4. Are there any limitations to the Monte Carlo calculation of π?

One limitation of the Monte Carlo method is that it can be computationally intensive, requiring a large number of random points to achieve high accuracy. Additionally, the accuracy may be affected by the quality of the random number generator used.

5. How is the Monte Carlo calculation of π used in real-world applications?

The Monte Carlo method has many real-world applications, including in finance, engineering, and physics. For example, it can be used to simulate and analyze the behavior of complex systems, such as stock markets or nuclear reactors. It can also be used in optimization problems, such as finding the most efficient design for a structure or process.

Similar threads

  • Programming and Computer Science
Replies
1
Views
753
  • Programming and Computer Science
Replies
1
Views
647
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
619
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
3K
  • Set Theory, Logic, Probability, Statistics
Replies
2
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
12
Views
2K
  • Atomic and Condensed Matter
Replies
3
Views
874
  • Programming and Computer Science
Replies
4
Views
621
Back
Top