mercredi 5 juin 2019

Fortran90: rand(1) not generating random numbers [duplicate]

This question already has an answer here:

I'm following along with an online fortran tutorial and am getting the same number for code that's supposed to generate random numbers, using rand(1) and call random_number.

This is the tutorial I'm following: https://youtu.be/__2UgFNYgf8?t=927

Our codes are nearly identical, but I'll repost it here for completeness:

program fortrantutorial
  real :: float_num = 1.111111111111111
  real :: float_num2 = 1.111111111111111
  double precision :: dbl_num = 1.1111111111111111d+0
  double precision :: dbl_num2 = 1.1111111111111111d+0
  real :: rand(1)
  integer :: low = 1, high = 10

  print "(f17.15)", float_num + float_num2
  print "(f17.15)", dbl_num + dbl_num2

  call random_number(rand)
  print "(f7.5)", rand
  print "(i2)", low + floor((high + 1 - low)*rand)

end program fortrantutorial

As you can see, the only difference in the code (as far as I can tell), is that I've added an extra line to print rand itself.

The output of this code is always:

2.222222222222222
0.99756
10

but the guy running the tutorial is getting a variety of random numbers; 5, 2, 4, 9...

I don't understand why he's getting different numbers than I am despite the code being practically identical. Is there something I'm omitting that selects a new seed? Why would that be automatic for his code but not mine?




Aucun commentaire:

Enregistrer un commentaire