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
Click For Summary
SUMMARY

The discussion focuses on the usage of the Fortran 90 subroutines random_seed and random_number. The user encountered an issue where the output of their program remained constant across executions, despite expectations of variability. The root cause was identified as the use of random_seed without arguments, which initializes the random number generator to a default value. To achieve different outputs, the user must provide a varying seed value, such as one derived from the system clock.

PREREQUISITES
  • Understanding of Fortran 90 programming language
  • Familiarity with random number generation concepts
  • Knowledge of subroutine usage in Fortran
  • Basic understanding of system time retrieval methods
NEXT STEPS
  • Learn how to implement random_seed with custom seed values in Fortran 90
  • Explore the Fortran 90 documentation on random_number for advanced usage
  • Investigate methods to retrieve system time in Fortran for seeding
  • Study debugging techniques for programs utilizing random number generation
USEFUL FOR

This discussion is beneficial for Fortran developers, particularly those working with random number generation and matrix computations, as well as programmers seeking to understand the implications of seed initialization in random number algorithms.

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   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
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