RANDOM_NUMBER and RANDOM_SEED help

I am trying to run a simple monte carlo program on the cluster at my school and am compiling using pgf90. One problem that I have run into is that the random number generator “RANDOM_NUMBER(HARVEST)” does not return a random variable via a seed based upon the clock time or anything, but rather produces the same set of random numbers each time the problem is run. This causes the exact same results when the problem is run simultaneous times, provided the same number of histories is used in each run.

I also tried adding the line “RANDOM_SEED()” on the preceding line to the “RANDOM_NUMBER()” call, but the results ended up with many “nan” values, thus something was amiss in this call. Do you have any suggestions as to a reliable random number (or pseudo-random number) generator, how to use RANDOM_SEED, or how to help resolve this issue in some other fashion (any compiler options, or other available functions or subroutines)? Thanks!

~Jack

Hi Jack,

The random number generator has the following behavior.

  1. Calling “RANDOM_NUMER” without setting the seed, will use a static default seed and will result in the same “random” numbers being generated every time your application runs.

  2. Calling “RANDOM_SEED()” without a seed value, will initialize the default seed to a number based on the current time. Subsequent calls to RANDOM_SEED, will reset the seed to this inital value. This feature was added to the 5.2 compilers. Prior versions would use the static default as above.

  3. Calling “RANDOM_SEED(SEED)” with a value for seed sets the default seed value to use. This is the prefered method since cases 1 and 2 are impementation dependent.

Hope this helps,
Mat

Example code:

! =================================
PROGRAM test_rand
  ! =================================

  REAL*8 X
  INTEGER N(2)   
  INTEGER time(8) 
  INTEGER seed(2)       

  WRITE(*,*) 'Case 1: A user calls for a random'
  WRITE(*,*) '        number before the seed is' 
  WRITE(*,*) '        set.  Should return the'
  WRITE(*,*) '        static default'
  CALL RANDOM_NUMBER(HARVEST = X)
  WRITE(*,*), 'VAL1a= ', X
  CALL RANDOM_NUMBER(HARVEST = X)
  WRITE(*,*), 'VAL1b= ', X
  CALL RANDOM_NUMBER(HARVEST = X)
  WRITE(*,*), 'VAL1c= ', X

  WRITE(*,*) 'Case 2: A user that call random seed without'
  WRITE(*,*) '        arguments should get a new random default'
  CALL RANDOM_SEED()
  CALL RANDOM_NUMBER(HARVEST = X)
  write(*,*), 'VAL2a= ', X  
  CALL RANDOM_NUMBER(HARVEST = X)
  write(*,*), 'VAL2b= ', X
  CALL RANDOM_NUMBER(HARVEST = X)
  write(*,*), 'VAL2c= ', X

  WRITE(*,*) 'CASE 3: A user that call random seed with'
  WRITE(*,*) '        arguments '
  call DATE_AND_TIME(values=time)     ! Get the current time
  seed(1) = time(4) * (360000*time(5) + 6000*time(6) + 100*time(7) + time(8))
  print *, 'SEED= ', seed(1)
  CALL RANDOM_SEED(PUT=seed)
  CALL RANDOM_NUMBER(HARVEST = X)
  write(*,*), 'VAL3a= ', X  
  CALL RANDOM_NUMBER(HARVEST = X)
  write(*,*), 'VAL3b= ', X
  CALL RANDOM_NUMBER(HARVEST = X)
  write(*,*), 'VAL3c= ', X

END PROGRAM test_rand