Consider the following piece of code. It gets the current random seed (= seed0
), modifies it (=seed1
), put the modified seed back, and then get immediately the random seed again (=seed2
). As far as I understand, we should have seed1 == seed2
so that the program should print 0, unless seed1
is not a valid seed.
program testrand
implicit none
integer :: n
integer, allocatable :: seed0(:), seed1(:), seed2(:)
! Initialize the seed, and get its size.
call random_seed()
call random_seed(size=n)
allocate (seed0(n), seed1(n), seed2(n))
! Get the current seed.
call random_seed(get=seed0)
! Modify the seed, and put it as the new seed.
seed1 = max(seed0 - 1, 1)
! In most cases, `seed1` should still be a valid seed, no?
call random_seed(put=seed1)
! Get the new seed.
call random_seed(get=seed2)
! Check whether the new seed is the one we put.
print *, maxval(abs(seed2 - seed1))
end program testrand
The aforementioned speculation seems correct with gfortran
, ifort
, g95
, flang
, nvfortran
, af95
(Absoft), and sunf95
(Oracle). However, with nagfor
(NAG Fortran Compiler Release 7.0(Yurakucho) Build 7036), the printed number is never 0 on my machine (Ubuntu 20.04).
If we do not modify seed0
but set seed1=seed0
, then all the compilers will print 0
as expected.
Did I overlook something about random_seed
?