Fortran Fortran 90, how do I use random_number and random_seed?

  • Thread starter Thread starter Animastryfe
  • Start date Start date
  • Tags Tags
    Fortran
Click For Summary
The discussion centers on the use of the Fortran subroutines random_seed and random_number for generating random matrices. The user expected different outputs with each execution of their program but consistently received the same result. This was due to calling random_seed without arguments, which initializes the random number generator to a default value, leading to identical outputs across runs. To achieve varied outputs, it is necessary to provide random_seed with different values each time, such as using the system clock. The user confirmed that this understanding resolved their issue and inquired about marking the thread as solved.
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?
 
Technology news on Phys.org
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 1 person
Thank you, this solves my problem. Is there a way to mark this thread as solved?
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

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
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 59 ·
2
Replies
59
Views
11K
  • · Replies 4 ·
Replies
4
Views
3K