Fortran 90, how do I use random_number and random_seed?

  • Fortran
  • Thread starter Animastryfe
  • Start date
  • Tags
    Fortran
In summary, the conversation revolves around the use of random_seed and random_number in a Fortran program. The main issue is that the output of the program is always the same, even after recompiling. The experts explain that random_seed is used to initialize the random number generator and that calling it with no arguments will use a default value, resulting in the same output every time. To get different random numbers, a different value must be passed to random_seed each time. The use of the system clock is suggested as a way to achieve this.
  • #1
Animastryfe
81
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
  • #2
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
  • #3
Thank you, this solves my problem. Is there a way to mark this thread as solved?
 

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

1. How do I generate random numbers in Fortran 90 using the random_number function?

The random_number function in Fortran 90 allows you to generate random real numbers between 0 and 1. To use this function, you must first declare a real variable and then call the random_number function with that variable as an argument. This will assign a random value to the variable each time the program is run.

2. How do I generate a specific range of random numbers in Fortran 90?

The random_number function only generates random numbers between 0 and 1. To generate a specific range of random numbers, you can multiply the output of the function by the range you desire and add the minimum value of the range. For example, to generate random numbers between 10 and 20, you would multiply the output by 10 and add 10 to it.

3. How do I ensure that the random numbers in my Fortran 90 program are truly random?

To ensure that the random numbers in your program are truly random, you should use the random_seed function. This function initializes the random number generator with a seed value, which is a starting point for the sequence of random numbers. It is important to change the seed value each time the program is run to ensure a different sequence of random numbers.

4. Can I use the same seed value for the random_seed function each time I run my Fortran 90 program?

No, it is not recommended to use the same seed value for the random_seed function each time the program is run. This can result in the same sequence of random numbers being generated each time, which defeats the purpose of using random numbers. It is best to use a different seed value each time or to use a system time-based seed value for greater randomness.

5. How can I ensure that the random numbers in my Fortran 90 program are reproducible?

If you need to produce the same sequence of random numbers each time the program is run, you can use the random_seed function with a specific seed value. This will initialize the random number generator with the same starting point each time, resulting in the same sequence of random numbers being generated. However, keep in mind that this goes against the purpose of using random numbers for most applications.

Similar threads

  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
4
Views
7K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
693
Replies
6
Views
1K
  • Programming and Computer Science
2
Replies
59
Views
9K
  • Programming and Computer Science
Replies
16
Views
1K
Back
Top