Sta 6106 – Computer-Intensive Methods in Statistics Some Sample fortran 77 Programs




Дата канвертавання19.04.2016
Памер20.11 Kb.
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 integral , 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 integral , using 2000 generated pseudorandom numbers.
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 .
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


База данных защищена авторским правом ©shkola.of.by 2016
звярнуцца да адміністрацыі

    Галоўная старонка