mercredi 18 mai 2016

Random number generator produces same sequence even though it's seeded

I'm relatively new to fortran and am trying to understand the RANDOM_NUMBER and RANDOM_SEED subroutines. The following code continually produces the same sequence of random numbers despite the fact that I seed the generator outside the DO LOOP at the top of the program.

  1 PROGRAM TEST
  2 
  3         IMPLICIT NONE
  4         
  5         INTEGER :: I, OUTPUT
  6         REAL :: R
  7 
  8         CALL RANDOM_SEED()
  9 
 10         DO I=1, 10
 11                 CALL RANDOM_NUMBER(R)
 12                 OUTPUT = I*R
 13                 PRINT *,'Random number ', I, ' = ',  OUTPUT
 14         END DO
 15 
 16 END PROGRAM TEST

Here is the output when I run the code

 Random number            1  =            0
 Random number            2  =            1
 Random number            3  =            2
 Random number            4  =            2
 Random number            5  =            1
 Random number            6  =            2
 Random number            7  =            0
 Random number            8  =            0
 Random number            9  =            3
 Random number           10  =            3

I get this exact sequence every time I run the code. I've even tried recompiling to see if the generator would reseed at compile time.




Aucun commentaire:

Enregistrer un commentaire