JuliaStats / Distributions.jl

A Julia package for probability distributions and associated functions.
Other
1.12k stars 418 forks source link

libRmath-dependent random numbers are not seeded by srand, thus not reproducible #599

Closed RainerEngelken closed 7 years ago

RainerEngelken commented 7 years ago

Distributions.jl uses libRmath to sample from various distributions e.g. Binomial, Poisson, Beta, Hypergeometric, Gamma and Dirichlet. libRmath uses the global stream of dSFMT, which is not seeded by srand. Thus, sampling from several distributions is not reproducible, which is problematic e.g. for scientific computing. For example, the following throws an error in Version 0.5.1 and 0.6.0-pre.beta.215 and 0.7.0-DEV.49 but not in 0.4.7:

using Distributions
srand(1)
u1=rand(Binomial(10^6,10^-2.))
srand(1)
u2=rand(Binomial(10^6,10^-2.))
assert(u1==u2)

This was problem was solved for version 0.4 by @rfourquet (https://github.com/JuliaLang/julia/issues/9124) but seems to have reappeared in version 0.5. A earlier solution for seeding dSFMT was:

dsfmt_srand(seed) = ccall(("dsfmt_gv_init_gen_rand","libdSFMT.so"), Void, (UInt32,), UInt32(seed))

but it does not to solve the problem both in version 0.5.1 and in version 0.6.0. I use version 0.12.5 of Distributions in Julia 0.5, 0.6 and 0.7 and version 0.11.1 in Julia 0.4.7.

andreasnoack commented 7 years ago

Dup of https://github.com/JuliaStats/Distributions.jl/issues/540

RainerEngelken commented 7 years ago

Ok, thanks. Just for the record if somebody has the same problem, the following procedure solved it: Delete Pkg.dir("Rmath","deps","usr") and Pkg.dir("Rmath","deps","src") and Pkg.dir("Rmath","deps","downloads") and Pkg.dir("Rmath","deps","deps.jl"), do sudo rm /usr/lib/libRmath-julia.so, sudo rm /usr/lib/libRmath-julia.so.2 then do Pkg.build("Rmath"). After restart julia, libRmath can be seeded by srand().