Random seed initialization
Janus Weil
janus@gcc.gnu.org
Sat Apr 6 15:03:00 GMT 2013
Hi Angelo,
> Surely I am missing something but any way I want to flag this for
> completeness.
>
> The following code uses the subroutine example for RANDOM_SEED, cited in GCC
> manual (current development):
>
> [...]
>
> Now, when I compile it using GCC-4.5.3, it prints different number sequences
> each time I run it.
>
> Using GCC 4.9.0 20130324 it prints always the same sequence.
>
> I have tested this on Cygwin with its GCC-4.5.3 packages,
>
> $ gfortran -v
> Using built-in specs.
> COLLECT_GCC=gfortran
> COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-pc-cygwin/4.5.3/lto-wrapper.exe
> Target: i686-pc-cygwin
> Configured with:
> /gnu/gcc/releases/respins/4.5.3-3/gcc4-4.5.3-3/src/gcc-4.5.3/configure
> --srcdir=/gnu/gcc/releases/respins/4.5.3-3/gcc4-4.5.3-3/src/gcc-4.5.3
> --prefix=/usr --exec-prefix=/usr --bindir=/usr/bin --sbindir=/usr/sbin
> --libexecdir=/usr/lib --datadir=/usr/share --localstatedir=/var
> --sysconfdir=/etc --datarootdir=/usr/share --docdir=/usr/share/doc/gcc4 -C
> --datadir=/usr/share --infodir=/usr/share/info --mandir=/usr/share/man -v
> --with-gmp=/usr --with-mpfr=/usr --enable-bootstrap
> --enable-version-specific-runtime-libs --libexecdir=/usr/lib --enable-static
> --enable-shared --enable-shared-libgcc --disable-__cxa_atexit --with-gnu-ld
> --with-gnu-as --with-dwarf2 --disable-sjlj-exceptions
> --enable-languages=ada,c,c++,fortran,java,lto,objc,obj-c++ --enable-graphite
> --enable-lto --enable-java-awt=gtk --disable-symvers --enable-libjava
> --program-suffix=-4 --enable-libgomp --enable-libssp --enable-libada
> --enable-threads=posix --with-arch=i686 --with-tune=generic
> --enable-libgcj-sublibs CC=gcc-4 CXX=g++-4 CC_FOR_TARGET=gcc-4
> CXX_FOR_TARGET=g++-4 GNATMAKE_FOR_TARGET=gnatmake
> GNATBIND_FOR_TARGET=gnatbind --with-ecj-jar=/usr/share/java/ecj.jar
> Thread model: posix
> gcc version 4.5.3 (GCC)
>
> and with a my build of 4.9.0 20130324,
>
> $ gfortran-4.9 -v
> Using built-in specs.
> COLLECT_GCC=gfortran-4.9
> COLLECT_LTO_WRAPPER=/usr/local/gfortran/libexec/gcc/i686-pc-cygwin/4.9.0/lto-wrapper.exe
> Target: i686-pc-cygwin
> Configured with: /home/angelo/work/gcc-4.9-20130324/configure
> --prefix=/usr/local/gfortran --program-suffix=-4.9
> --enable-languages=c,c++,fortran --enable-checking=release
> --enable-threads=posix --enable-libgomp --with-arch=native
> --with-tune=native --with-fpmath=sse --disable-bootstrap
> --disable-libmudflap --disable-shared
> Thread model: posix
> gcc version 4.9.0 20130324 (experimental) (GCC)
>
>
> I suspect this behavior is to be expected for a snapshot...
Not really!
> May you confirm?
I just tried the program with four different versions (on
x86_64-unknown-linux-gnu):
gcc version 4.3.4 [gcc-4_3-branch revision 152973] (SUSE Linux)
gcc version 4.7.2 20130205 [gcc-4_7-branch revision 195759] (SUSE Linux)
gcc version 4.8.0 (SUSE Linux)
gcc version 4.9.0 20130404 (experimental) [trunk revision 197305] (GCC)
and all of them give me different sequences on each run.
Maybe you could post the output of this enhanced version (which also
prints the clock and seed values):
program rnd_init
implicit none
real :: rnd(9)
call init_random_seed()
call random_number(rnd)
print *,"random: ",rnd
contains
SUBROUTINE init_random_seed()
INTEGER :: i, n, clock
INTEGER, DIMENSION(:), ALLOCATABLE :: seed
CALL RANDOM_SEED(size = n)
print *,"n=",n
ALLOCATE(seed(n))
CALL SYSTEM_CLOCK(COUNT=clock)
print *,"clock:", clock
seed = clock + 37 * (/ (i - 1, i = 1, n) /)
CALL RANDOM_SEED(PUT = seed)
print *,"seed: ",seed
DEALLOCATE(seed)
END SUBROUTINE init_random_seed
end program rnd_init
Might be a platform-specific problem (with SYSTEM_CLOCK?!?) ...
Cheers,
Janus
More information about the Fortran
mailing list