Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortran 90, how do I use random_number and random_seed?

  1. Nov 16, 2011 #1
    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 (Text):
    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?
     
  2. jcsd
  3. Nov 16, 2011 #2

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    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
     
  4. Nov 16, 2011 #3
    Thank you, this solves my problem. Is there a way to mark this thread as solved?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Fortran 90, how do I use random_number and random_seed?
Loading...