Random seed initialization
Angelo Graziosi
angelo.graziosi@alice.it
Sat Apr 6 15:32:00 GMT 2013
Ciao Janus,
Il 06/04/2013 17.03, Janus Weil ha scritto:
>
>>
>> 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!
>
> [...]
>
> 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
>
When I compile it with GCC-4.5.3 the results are:
$ ./rnd_init.out !! 1st run
n= 8
clock: 1609234520
seed: 1609234520 1609234557 1609234594 1609234631 1609234668
1609234705 1609234742 1609234779
random: 0.20771700 0.70008731 5.39250970E-02 0.93139762
0.64063454 0.79792714 0.44434732 0.53217107
0.92274189
$ ./rnd_init.out !! 2nd run
n= 8
clock: 1609240114
seed: 1609240114 1609240151 1609240188 1609240225 1609240262
1609240299 1609240336 1609240373
random: 0.21628165 0.50345588 0.47903579 0.87107950
0.64046717 0.82357150 0.49985659 0.13067204
0.22462136
$ ./rnd_init.out !! 3rd run
n= 8
clock: 1609245348
seed: 1609245348 1609245385 1609245422 1609245459 1609245496
1609245533 1609245570 1609245607
random: 0.71807641 0.52906609 5.59192896E-03 0.52994502
0.36918062 0.28023791 8.55535269E-03 0.15218848
0.52063912
etc..
Using my build of GCC-4.9.0 20130324, each run produces always this
$ ./rnd_init.out
n= 12
clock: 0
seed: 0 37 74 111 148
185 222 259 296 333 370
407
random: 0.301754534 0.380439878 0.926293135
0.672118008 0.237815678 0.638214409 0.320253670
0.351599574 0.865273237
It seems the call to SYSTEM_CLOCK() is failing..
>
> Might be a platform-specific problem (with SYSTEM_CLOCK?!?) ...
... or my build of GCC is not so good (see above for configure options)...
Ciao,
Angelo.
More information about the Fortran
mailing list