0% found this document useful (0 votes)
213 views3 pages

Hand Out Fortran 77

The document contains 6 FORTRAN 77 programs that demonstrate using pseudorandom number generation and Monte Carlo integration methods to approximate integrals and calculate values of mathematical constants like pi. The programs generate random numbers, calculate sums of random numbers weighted by functions, and use the results to estimate integral values and pi.

Uploaded by

Dj Xperia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
213 views3 pages

Hand Out Fortran 77

The document contains 6 FORTRAN 77 programs that demonstrate using pseudorandom number generation and Monte Carlo integration methods to approximate integrals and calculate values of mathematical constants like pi. The programs generate random numbers, calculate sums of random numbers weighted by functions, and use the results to estimate integral values and pi.

Uploaded by

Dj Xperia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

=== STA 6106 Computer-Intensive Methods in Statistics

Some Sample FORTRAN 77 Programs ====


The following FORTRAN 77 program calls a random number generating function to generate a
single random number between 0 and 1, using an entered seed value, and entered values of the
generator constants.
PROGRAM RANDGENR
PRINT*, 'Enter seed value, and values of A and B:'
READ*, SEED, A, B
PRINT*, 'Random number is ', RANDOM(SEED, A, B)
END
FUNCTION RANDOM(X0, CONST, DIVIS)
X = MOD(CONST*X0,DIVIS)
RANDOM = X/DIVIS
END

Note that all FORTRAN command lines must begin at column 7 or after, and cannot extend past
column 78. The first 5 columns are reserved for line numbers.
The following FORTRAN 77 program generates a list of 100 random numbers between 0 and 1,
using a DO loop.
PROGRAM RANDGENR
PRINT*, 'Enter seed value, and values of A and B:'
READ*, SEED, A, B
J = 1
X = MOD(A*SEED,B)
SEED = X
DO WHILE (J .LE. 100)
X = MOD(A*SEED,B)
RANDOM = X/B
PRINT*, RANDOM
SEED = X
J = J + 1
ENDDO
END

The following FORTRAN 77 program uses the Law of Large Numbers to approximate the
1

integral

dx , using 2000 pseudorandom numbers between 0 and 1. Note that the values of

the generator constants are written into the program; the only input is the seed value.
PROGRAM RANDGENR
PRINT*, 'Enter seed value: '
READ*, SEED
J = 1
A = 16807
B = 2147483647
X = MOD(A*SEED,B)
SEED = X
SUM = 0

DO WHILE (J .LE. 2000)


X = MOD(A*SEED,B)
RANDOM = X/B
PRINT*, RANDOM
SUM = SUM + (RANDOM*RANDOM)
SEED = X
J = J + 1
ENDDO
SUM = SUM/2000
PRINT*, 'Integral is approximately '
PRINT*, SUM
END

The following FORTRAN 77 program uses the Law of Large Numbers to approximate the
z
1

1
e 2 dz 1 0 , using 2000 generated pseudorandom numbers.
integral
2
0
2

PROGRAM STDNORM
REAL PI
PRINT*, 'Enter seed value: '
READ*, SEED
PI = 3.141592654
J = 1
A = 16807
B = 2147483647
X = MOD(A*SEED,B)
SEED = X
SUM = 0
DO WHILE (J .LE. 2000)
X = MOD(A*SEED,B)
RANDOM = X/B
PRINT*, RANDOM
ARGMNT = (1/RANDOM) - 1
SUM = SUM + ((EXP(-0.5*(ARGMNT**2)))/SQRT(2*PI))/(RANDOM**2)
SEED = X
J = J + 1
ENDDO
SUM = SUM/2000
PRINT*, 'Integral is approximately '
PRINT*, SUM
END

The following FORTRAN 77 program calculates the value of 4 4 .


PROGRAM STDNORM
REAL PI
PRINT*, 'Enter seed value: '
READ*, SEED
PI = 3.141592654
J = 1
A = 16807
B = 2147483647
X = MOD(A*SEED,B)

SEED = X
SUM = 0
DO WHILE (J .LE. 2000)
X = MOD(A*SEED,B)
RANDOM = X/B
PRINT*, RANDOM
ARGMNT = -4 + (8*RANDOM)
SUM = SUM + (8*(EXP(-0.5*(ARGMNT**2)))/SQRT(2*PI))
SEED = X
J = J + 1
ENDDO
SUM = SUM/2000
PRINT*, 'Integral is approximately '
PRINT*, SUM
END

The following FORTRAN 77 program estimates the value of , using the algorithm discussed in
class. Note that the number of pairs of pseudorandom numbers needs to be quite large to get a
value close to .
PROGRAM ESTPI
PRINT*, 'Enter seed values, and sample size: '
READ*, SEED1, SEED2, N
A = 16807
B = 2147483647
J = 1
COUNT = 0
X1 = MOD(A*SEED1,B)
X2 = MOD(A*SEED2,B)
SEED1 = X1
SEED2 = X2
DO WHILE (J .LE. N)
X1 = MOD(A*SEED1,B)
X2 = MOD(A*SEED2,B)
RANDOM1 = X1/B
RANDOM2 = X2/B
PRINT*, RANDOM1, RANDOM2
SEED1 = X1
SEED2 = X2
Z = ((2*RANDOM1-1)**2)+((2*RANDOM2-1)**2)
IF (Z .LE. 1) COUNT = COUNT + 1
J = J + 1
ENDDO
PIEST = 4*COUNT/N
PRINT*, 'Estimated value of PI = ', PIEST
END

You might also like