! ! File: ~/numerical/random.f90 ! ! Program to test f90 intrinsics to generate random numbers, ! (somewhat) randomly seeded off the system clock. ! ! C.G. (12-98) ! PROGRAM random IMPLICIT NONE INTEGER :: i, msec, n, time_info(8) REAL :: num(10) ! ! Get all 8 integer fields off the date/time function: ! time_info(7) = seconds of the minute (range 0 to 60) ! time_info(8) = milliseconds of the second (range 0 to 999) ! CALL DATE_AND_TIME( VALUES = time_info ) msec = 1000 * time_info(7) + time_info(8) ! a somewhat random integer CALL RANDOM_SEED( SIZE = n ) ! get the number of integers used for the seed CALL RANDOM_SEED( PUT = (/ ( i * msec, i = 1, n ) /) ) ! give a proper seed CALL RANDOM_NUMBER( num ) ! generate a sequence of 10 pseudo-random numbers PRINT '(10F5.2 )', num END PROGRAM random ! ! Sample output from successive runs: ! ! 0.34 0.79 0.70 0.80 0.50 0.80 0.50 0.21 0.90 0.67 ! 0.82 0.22 0.25 0.03 0.34 0.34 0.26 0.07 0.29 1.00 ! 0.45 0.83 0.71 0.77 0.17 0.43 0.32 0.90 0.27 0.24 ! ! Note: If you don't re-seed (and just directly call 'RANDOM_NUMBER()', ! then you get the same sequence every time (which may or may not ! be a problem ... depending on what you're doing) !