1

Here is a program I'm using to generate random numbers:

program randtest
    implicit none

    real rands(5)

    call random_number(rands)
    print*, rands

end program randtest

Note that I'm not initializing with a seed using random_seed as that is my intention. What I've learnt about pseudo-random number generators, like the one Fortran uses is that they start at a seed value which is either provided or chosen by default. So if the seed is not changed, the sequence of random numbers produced remains the same.

However, when I run this code on two different platforms using gfortran, I see different behaviour.

  • On Ubuntu 18.04 LTS, gfortran 7.5.0, it generates a different sequence of numbers each time I run the compiled code.
  • On Windows 10 (MinGW), gfortran 6.3.0, it generates the same sequence no matter how many times I run it.

Any insight about why this happens? Is it because in Ubuntu, somehow a different seed is chosen by default on each run? If so, why, when both are compiled using gfortran?

amzon-ex
  • 1,645
  • 1
  • 6
  • 28
  • 1
    The point of providing a seed is to make it reproducible. I am not sure why you would expect reproducibility in the absence of an explicit seed. "chosen by default" could depend upon the exact moment in time that the code is run. – John Coleman Oct 21 '20 at 12:32
  • @JohnColeman I agree, but why is the same sequence produced on Windows, then? I'm looking to find out the reasons for the difference, and why do I get such behaviour. – amzon-ex Oct 21 '20 at 12:34
  • If it isn't explicitly documented, you would need to delve into the source of `gfortan` to see where the difference in behavior comes from. – John Coleman Oct 21 '20 at 12:36
  • 1
    @JohnColeman, [gfortran 7](https://stackoverflow.com/a/31922538/3157076). – francescalus Oct 21 '20 at 12:42
  • Seems related: https://stackoverflow.com/q/18880654/4996248 . If so, the problem (if it is an actual problem) is traceable to how `MinGW` approaches randomness. – John Coleman Oct 21 '20 at 12:45
  • 1
    I've suggested that this is a duplicate of another question. That other question is _slightly_ different in its focus but the essential part is that: the behaviour is entirely implementation dependent, even depending on _versions_ of compilers used. If you want to be consistent you need to code that explicitly. If you think that question doesn't help, please say so. – francescalus Oct 21 '20 at 12:45
  • 1
    And if you do say so, you also have to provide the versions of gfortran, not just of the operating system. francescalus already pointed out the change in how `random_seed()` without arguments works in different versions of gfortran. What happens if it is not called at all is implementation dependent but can easily depend on the version in a similar way. – Vladimir F Героям слава Oct 21 '20 at 13:37
  • @francescalus It helps get me an idea, thank you. – amzon-ex Oct 21 '20 at 15:02
  • 1
    @francescalus Added. As you've explained, it is probably tied to the version of gfortran. I updated to MinGW gfortran 8.1.0 yesterday, and now the random number sequences are different each time. – amzon-ex Oct 23 '20 at 06:03

0 Answers0