http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52879
Bug #: 52879
Summary: Pathological reseeding of PRNG generator genernates
poor sequence
Classification: Unclassified
Product: gcc
Version: 4.6.3
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libfortran
AssignedTo: [email protected]
ReportedBy: [email protected]
It is possible to reseed the gfortran's PRNG with sets of
seeds that generate poor sequences of random numbers. This
problem can to light from the discussion in
https://groups.google.com/group/comp.lang.fortran/browse_frm/thread/660c46a52644a8ad?hl=en#
Consider the program
program foo
implicit none
character(len=40), parameter :: fmt = '(A,12(I0,1X),A,F10.8)'
integer i, seed_size, date(8)
integer, allocatable :: new_seed(:)
real x
call random_seed(size=seed_size)
allocate(new_seed(1:seed_size))
do i = 6, 12, 2
call date_and_time(values=date)
new_seed = 1
new_seed(1) = i * (sum(date(1:3)) + sum(date(5:8)))
call random_seed(put=new_seed)
call random_number(x)
write(*,fmt) 'seeds = [', new_seed, '], x = ', x
call sleep(i)
end do
print *
do i = 6, 12, 2
call date_and_time(values=date)
new_seed = 1
new_seed(12) = i * (sum(date(1:3)) + sum(date(5:8)))
call random_seed(put=new_seed)
call random_number(x)
write(*,fmt) 'seeds = [ ', new_seed, '], x = ', x
call sleep(i)
end do
deallocate(new_seed)
end program foo
With 4.6, 4.7, and trunk, I get
troutmask:sgk[239] gfc46 -o bar bar.f90 && ./bar
seeds = [15852 1 1 1 1 1 1 1 1 1 1 1 ], x = 0.76322097
seeds = [21200 1 1 1 1 1 1 1 1 1 1 1 ], x = 0.76322097
seeds = [26590 1 1 1 1 1 1 1 1 1 1 1 ], x = 0.76322097
seeds = [31332 1 1 1 1 1 1 1 1 1 1 1 ], x = 0.76322097
seeds = [ 1 1 1 1 1 1 1 1 1 1 1 15744 ], x = 0.76526332
seeds = [ 1 1 1 1 1 1 1 1 1 1 1 21048 ], x = 0.76410544
seeds = [ 1 1 1 1 1 1 1 1 1 1 1 26400 ], x = 0.76371950
seeds = [ 1 1 1 1 1 1 1 1 1 1 1 31812 ], x = 0.76429844
It would seem plausible to an ordinary user that if a
single seed value is changed, the generated sequence
will (significantly?) change. In the first case above,
the sequences are identical while in the second case
there appears to be a correlation among the low (high)
order bits of the initially generated values.
random_seed currently scrambles/unscrambles user
specified seeds to hopefully prevent a bad set of
seed values. One possible fix would be to XOR the
user specified seeds with the default seeds before
doing the scrambling. On output, the seeds would
be unscrambled and then again XOR'd with the
default seeds.