A sample random number Fortran program |
|
Here is a working Sun Fortran (f77) program to demonstrate non-repeating pseudorandom sequences.
c--
c rand.f: A sample program to generate a pseudorandom number sequence.
c This program prints ten numbers between 0.0 and 1.0, and it also
c prints a different sequence each time (unless you run it a LOT of
c times---the rand() functon is, after all, only pseudorandom).
c--
program rand
real rand ! Declare the type of the rand() function
integer i ! Counts random numbers
integer*4 timeArray(3) ! Holds the hour, minute, and second
c--
c In order to get a different sequence each time, we initialize the
c seed of the random number function with the sum of the current
c hour, minute, and second.
c--
call itime(timeArray) ! Get the current time
i = rand ( timeArray(1)+timeArray(2)+timeArray(3) )
c--
c Calling rand() with an argument of zero generates the next number
c in sequence.
c--
do i = 1, 10
print *, rand(0)
end do
stop
end