# \`random\_seed(get)\` immediately after \`random\_seed(put)\`: behavior of various compilers

**URL:** https://fortran-lang.discourse.group/t/random-seed-get-immediately-after-random-seed-put-behavior-of-various-compilers/1918
**Category:** Help
**Created:** [September 22, 2021, 5:13pm UTC](https://fortran-lang.discourse.group/t/random-seed-get-immediately-after-random-seed-put-behavior-of-various-compilers/1918 "2021-09-22T17:13:58Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![zaikunzhang](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/zaikunzhang/32/785_2.png) [@zaikunzhang](https://fortran-lang.discourse.group/u/zaikunzhang)
#### Post date: [September 22, 2021, 5:13pm UTC](https://fortran-lang.discourse.group/t/random-seed-get-immediately-after-random-seed-put-behavior-of-various-compilers/1918/1 "2021-09-22T17:13:58Z")

</div>

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

```auto
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`?

---

<div class="post-metadata">

### Author: ![zaikunzhang](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/zaikunzhang/32/785_2.png) [@zaikunzhang](https://fortran-lang.discourse.group/u/zaikunzhang)
#### Post date: [September 22, 2021, 5:45pm UTC](https://fortran-lang.discourse.group/t/random-seed-get-immediately-after-random-seed-put-behavior-of-various-compilers/1918/3 "2021-09-22T17:45:15Z")

</div>

> [@kargl](#):
>
> `SEED2 need not equal SEED1.`

> [@](#):
>
> When the values differ, the use of either value as the PUT argument in a subsequent call to RANDOM\_SEED shall result in the same sequence of pseudorandom numbers being generated. For example, after execution of the statements

Thanks, @kargl. I see. So, when receiving a seed from `random_seed(put)`, the compiler may translate it into another one that has a different value but the same effect as a seed.

Totally unrelated, but this reminds me of the following remark made by Johann Wolfgang von Goethe about mathematicians.

> [@](#):
>
> Die Mathematiker sind eine Art Franzosen: redet man zu ihnen, so übersetzen sie es in ihre Sprache, und dann ist es alsobald ganz etwas anders.

(Mathematicians are like Frenchmen: whatever you say to them they translate into their own language and forthwith it is something entirely different.)

It is clear now. Thanks.
