Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with random number routine 1

Status
Not open for further replies.

TheKKK

Technical User
Mar 21, 2009
10
GR
Im trying to call a sequence of random numbers created by a function called RANF() as seen below.
The problem is that I get the same number (1.0) when using the code below and im wondering why:

program LOOP

INTEGER(KIND=8) :: i
REAL(KIND=8) :: RANF

do i=1,20

number = RANF()
PRINT*,nymber

enddo

end program

FUNCTION RANF()
DATA IA/16807/,IC/2147483647/,IQ/127773/,IR/2836/
COMMON /CSEED/ ISEED
IH = ISEED/IQ
IL = MOD(ISEED,IQ)
IT = IA*IL-IR*IH
IF(IT.GT.0) THEN
ISEED = IT
ELSE
ISEED = IC+IT
END IF
RANF = ISEED/FLOAT(IC)
RETURN
END FUNCTION


 
At a guess

1) declare ranf as external in program
2) declare ranf as real*8 function in the definition
 
Hi TheKKK

I modified the program a little bit for my F77 compiles, put the COMMON statelemt in the main program also, initialized ISEED with a number, and defined iseed (integer*4) and number (real*8) in the main program. Now it seems to work:

program LOOP

c INTEGER(KIND=8) :: i
c REAL(KIND=8) :: RANF

INTEGER*4 i
INTEGER*4 iseed
REAL*8 RANF
REAL*8 number

COMMON /CSEED/ ISEED
iseed = 99999

do i=1,20
number = RANF()
PRINT *, number
enddo

end


FUNCTION RANF()
REAL*8 RANF
COMMON /CSEED/ ISEED
DATA IA/16807/,IC/2147483647/,IQ/127773/,IR/2836/
IH = ISEED/IQ
IL = MOD(ISEED,IQ)
IT = IA*IL-IR*IH
IF(IT.GT.0) THEN
ISEED = IT
ELSE
ISEED = IC+IT
END IF
RANF = ISEED/FLOAT(IC)
RETURN
END

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top