JuliaRandom / RandomNumbers.jl

Random Number Generators for the Julia Language.
https://juliarandom.github.io/RandomNumbers.jl/stable
Other
97 stars 23 forks source link

Xorshift128Plus has several catastrophically bad seeds #78

Closed c42f closed 3 years ago

c42f commented 3 years ago

It seems there are several extremely poor seeds for Xorshift128Plus which result in repeating sequences. For example, seed 512:

julia> rand(Xorshifts.Xorshift128Plus(512), 30)
30-element Vector{Float64}:
 0.12500214576741975
 0.1250021457673638
 0.015626089647719832
 0.15625003352785027
 0.2656260896476079
 0.12500214576747748
 0.015627162531226313
 0.15625217929502666
 0.14062608964765588
 1.1368683772161603e-13
 1.0728837338547237e-6
 2.1457675316582936e-6
 0.12500214576741975
 0.1250021457673638
 0.015626089647719832
 0.15625003352785027
 0.2656260896476079
 0.12500214576747748
 0.015627162531226313
 0.15625217929502666
 0.14062608964765588
 1.1368683772161603e-13
 1.0728837338547237e-6
 2.1457675316582936e-6
 0.12500214576741975
 0.1250021457673638
 0.015626089647719832
 0.15625003352785027
 0.2656260896476079
 0.12500214576747748

Examining the first million seeds, we can easily find several such examples which seem to fail in a similar way:

julia> for i = 1:1_000_000
           xs = rand(Xorshifts.Xorshift128Plus(i), 30)
           n = length(unique(xs))
           if n != length(xs)
               @info "Repeated numbers for $i (length $n)"
           end
       end
[ Info: Repeated numbers for 512 (length 12)
[ Info: Repeated numbers for 1024 (length 12)
[ Info: Repeated numbers for 1536 (length 12)
[ Info: Repeated numbers for 2048 (length 12)
[ Info: Repeated numbers for 2560 (length 12)
[ Info: Repeated numbers for 3072 (length 12)
[ Info: Repeated numbers for 3584 (length 12)
c42f commented 3 years ago

It seems that the authors suggest splitmix for initialization:

http://prng.di.unimi.it/ http://prng.di.unimi.it/splitmix64.c

Though even using this wouldn't change the fact that there's certain nonzero states which generate very short repeated sequences.

sunoru commented 3 years ago

Thank you for bringing this up! I did realize the problem before, but haven't taken efforts to fix it.

Actually, Xoshiro256Plus (which is the more recommended) has already used splitmix for initialization. Maybe I should use it for every RNG that performs bad with small seeds.

peteroupc commented 3 years ago

For your information, S. Vigna (@vigna) has discussed this point that affects linear PRNGs combined with weak scramblers (such as those that end in a single +):

sunoru commented 3 years ago

Solved in the new release.