Fortran 90, how do I use random_number and random_seed?

  • Fortran
  • Thread starter Animastryfe
  • Start date
  • #1

Main Question or Discussion Point

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?
 

Answers and Replies

  • #2
AlephZero
Science Advisor
Homework Helper
6,994
291
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
 
  • #3
Thank you, this solves my problem. Is there a way to mark this thread as solved?
 

Related Threads on Fortran 90, how do I use random_number and random_seed?

Replies
3
Views
1K
Replies
3
Views
6K
Replies
9
Views
6K
  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
7
Views
4K
  • Last Post
Replies
1
Views
1K
Replies
7
Views
5K
  • Last Post
Replies
13
Views
18K
  • Last Post
Replies
1
Views
2K
Top