Based on what I understood from the description of RANDOM_SEED Subroutine here, https://gcc.gnu.org/onlinedocs/gfortran/RANDOM_005fSEED.html
I tried to get an array of size n by taking n as an input argument. However, I always get array of a fixed size (n = 33) no matter what I input in the argument of the executable. What am I doing wrong?
program test_random_seed
implicit none
integer, allocatable :: seed(:)
integer :: n
Character(len=2) :: inputnum
call GETARG(1, inputnum)
read (inputnum, *) n
call random_seed(size = n)
allocate(seed(n))
call random_seed(get=seed)
write (*, *) seed
deallocate(seed)
end program test_random_seed
Outputs:
./test_random_seed.out 6
270997589 -288181887 1948167863 -899467666 856399949 -616956193 -393084398 -169799794 -2106914961 -1327699024 -611308309 -956136276 -1047101846 1405583576 -2030082380 1642309723 -964872898 -478908624 486119102 1947066547 -722265400 -268681072 366932413 -791652926 1961977905 -349345081 465186042 808162632 -129440729 -683435376 1183730510 264749721 0
./tes_trandom_seed.out 4
911554020 -223715509 -703604837 -1861804415 1952830407 -1126364511 -1115643071 -1251893035 1013863844 1767892501 1999133695 -1456139371 -1813327006 1135316397 -2034112324 -1863710425 749654783 1021195219 1479800515 882047462 -1297200646 1932542969 -1428989180 1078641551 -431962594 -1403398736 620407660 472937510 1677653929 1187629179 -357974360 953325445 0
My understanding of your last comment is that you are trying to initialize n
independent pseudorandom sequences and give them individual seeds. That is not possible in standard Fortran using random_seed()
. The array that random_seed()
uses is a single seed for a single sequence. The size of the array is fixed to specify the exact number of bits necessary for that single seed.
When using coarrays, it is possible to use random_init()
to request an independent sequence for each image.
You claim you are using the Mersenne twister. The Fortran standard does not specify the algorithm used for the intrinsic pseudorandom generator. Older versions of gfortran did IIRC use this algorithm, but newer versions are using a different one.
If you are using some third-party Mersenne twister library, then you have to use the seeding subroutines supplied by that library. Using random_seed()
only effects the intrinsic pseudorandom generator.
Please note that there is also no mechanism ta call random_number()
for independent sequences. What happens, when you call random_number()
from multiple threads is processor-dependent and you should consult the compiler manual.