Next / Previous / Index / TCC Help System / Publications / Site map / NM Tech homepage

A sample random number Fortran program

Tech Computer Center logo

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

See also: Generating random numbers in Fortran; Hints for Fortran programmers
Previous: Scaling random numbers in Fortran
Site map
Index: Keyword index to help pages
Help: New Mexico Tech Computer Center: Help System
TCC Publications
Home: About New Mexico Tech

John Shipman, john@nmt.edu

Last updated: 1995/12/19 18:58:09 UT
URL: http://www.nmt.edu/tcc/help/lang/fortran/rand.html