diff --git a/base/random.jl b/base/random.jl index fee039d6fe9db..6aa87aa50ff00 100644 --- a/base/random.jl +++ b/base/random.jl @@ -13,30 +13,27 @@ abstract AbstractRNG type MersenneTwister <: AbstractRNG state::DSFMT_state - seed::Union(Uint32,Vector{Uint32}) + seed::Vector{Uint32} - function MersenneTwister(seed::Vector{Uint32}) + function MersenneTwister(seed::Union(Integer,Vector{Uint32})) state = DSFMT_state() - dsfmt_init_by_array(state, seed) - return new(state, seed) + return srand(new(state), seed) end - MersenneTwister(seed=0) = MersenneTwister(make_seed(seed)) -end - -function srand(r::MersenneTwister, seed) - r.seed = seed - dsfmt_init_gen_rand(r.state, seed) - return r + MersenneTwister() = MersenneTwister(0) end ## initialization -function srand() +__init__() = srand() + +## make_seed() + +function make_seed() @unix_only begin try - srand("/dev/urandom") + return make_seed("/dev/urandom", 4) catch println(STDERR, "Entropy pool not available to seed RNG; using ad-hoc entropy sources.") seed = reinterpret(Uint64, time()) @@ -44,26 +41,18 @@ function srand() try seed = hash(seed, parseint(Uint64, readall(`ifconfig` |> `sha1sum`)[1:40], 16)) end - srand(seed) + return make_seed(seed) end end @windows_only begin a = zeros(Uint32, 2) win32_SystemFunction036!(a) - srand(a) + return make_seed(a) end end -__init__() = srand() - -## srand() - -function srand(seed::Vector{Uint32}) - global RANDOM_SEED = seed - dsfmt_gv_init_by_array(seed) -end -srand(n::Integer) = srand(make_seed(n)) +make_seed(seed::Vector{Uint32}) = seed function make_seed(n::Integer) n < 0 && throw(DomainError()) @@ -77,14 +66,34 @@ function make_seed(n::Integer) end end -function srand(filename::String, n::Integer) +function make_seed(filename::String, n::Integer) open(filename) do io a = Array(Uint32, int(n)) read!(io, a) - srand(a) + a end end -srand(filename::String) = srand(filename, 4) + +## srand() + +srand() = srand(make_seed()) + +srand(r::MersenneTwister) = srand(r, make_seed()) + +function srand(seed::Union(Integer, Vector{Uint32})) + global RANDOM_SEED = make_seed(seed) + dsfmt_gv_init_by_array(RANDOM_SEED) +end + +function srand(r::MersenneTwister, seed::Union(Integer, Vector{Uint32})) + r.seed = make_seed(seed) + dsfmt_init_by_array(r.state, r.seed) + return r +end + +srand(filename::String, n::Integer=4) = srand(make_seed(filename, n)) + +srand(r::MersenneTwister, filename::String, n::Integer=4) = srand(r, make_seed(filename, n)) ## random floating point values diff --git a/doc/stdlib/base.rst b/doc/stdlib/base.rst index e237f0af810dc..e3b8521fbcb72 100644 --- a/doc/stdlib/base.rst +++ b/doc/stdlib/base.rst @@ -3929,7 +3929,7 @@ Random number generation in Julia uses the `Mersenne Twister library