Fortran 90, how do I use random_number and random_seed?

  • Context: Fortran 
  • Thread starter Thread starter Animastryfe
  • Start date Start date
  • Tags Tags
    Fortran
Animastryfe
Messages
80
Reaction score
0
Hello,

I was given a program written by someone else that uses random_seed and random_number to generate a matrix. I thought the output of that program should change because each execution of that program should use a different random number to create the matrix, but the output is always the same, even after I recompile the program.

So I am trying to figure out how random_seed and random_number works in fortran. I'm using the example program given on the random_number page:

Code:
program test_random_number
   REAL(8) :: r
   CALL random_seed()
   CALL RANDOM_NUMBER(r)
   print *, r
end program

This should give me a different number each time I execute it, right? But it isn't. It gives me the same number even after I recompile. I did not specify any arguments for random_seed because the program I was working with did not specify any arguments for random_seed.

What am I missing about how these two subroutines work?
 
If you are debugging a program that uses random numbers, often you want to use the same sequence of random numbers for every run, otherwise it's hard to follow what is going on. You do that by using random_seed to initialize the random number generator with the same value(s) for every run.

The documentaion says if you call random_seed with no arguments, it is initialized to a "default value" so I guess your implementation always uses the same default value.

If you want different random numbers every time, you need to call random_seed with a different value every time. For example you can get a number from the system clock, as in
http://gcc.gnu.org/onlinedocs/gfortran/RANDOM_005fSEED.html
 
  • Like
Likes   Reactions: 1 person
Thank you, this solves my problem. Is there a way to mark this thread as solved?
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 3 ·
Replies
3
Views
7K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 59 ·
2
Replies
59
Views
12K
  • · Replies 4 ·
Replies
4
Views
3K