From bf8c4521128d98f000a80f98e4df7795f659ae29 Mon Sep 17 00:00:00 2001 From: Alessandro Andrioni Date: Tue, 19 Nov 2013 13:43:01 -0200 Subject: [PATCH] srand() now seeds both RNGs --- base/client.jl | 1 - base/gmp.jl | 11 +---------- base/mpfr.jl | 8 ++++---- base/random.jl | 6 ++++++ test/random.jl | 22 ++++++++++++++++++++++ 5 files changed, 33 insertions(+), 15 deletions(-) diff --git a/base/client.jl b/base/client.jl index 08031f8467c17..15d6632834597 100644 --- a/base/client.jl +++ b/base/client.jl @@ -368,7 +368,6 @@ function _start() LinAlg.init() Sys.init() GMP.gmp_init() - GMP.rand_init() global const CPU_CORES = Sys.CPU_CORES init_profiler() diff --git a/base/gmp.jl b/base/gmp.jl index 46152df8f7513..83ddf8c0b525b 100644 --- a/base/gmp.jl +++ b/base/gmp.jl @@ -483,7 +483,7 @@ function randu(randstate::BigRNG, k::BigInt) (Ptr{BigInt}, Ptr{BigRNG}, Ptr{BigInt}), &z, &randstate, &k) z end -randu(k::BigInt) = randu(DEFAULT_BIGRNG, k) +randu(k::BigInt) = randu(Base.Random.DEFAULT_BIGRNG, k) srand(r::BigRNG, seed) = srand(r, convert(BigInt, seed)) function srand(randstate::BigRNG, seed::Vector{Uint32}) @@ -504,13 +504,4 @@ function srand(randstate::BigRNG, seed::BigInt) return end -# Initialize the default BigRNG -function rand_init() - s = big(0) - for i in Base.Random.RANDOM_SEED - s = s << 32 + i - end - global DEFAULT_BIGRNG = BigRNG(s) -end - end # module diff --git a/base/mpfr.jl b/base/mpfr.jl index 3264126bde89e..aa82efc6f96c1 100644 --- a/base/mpfr.jl +++ b/base/mpfr.jl @@ -747,7 +747,7 @@ function rand(::Type{BigFloat}, randstate::BigRNG) &z, &randstate, ROUNDING_MODE[end]) z end -rand(::Type{BigFloat}) = rand(BigFloat, Base.GMP.DEFAULT_BIGRNG) +rand(::Type{BigFloat}) = rand(BigFloat, Base.Random.DEFAULT_BIGRNG) function rand!(r::BigRNG, A::Array{BigFloat}) for i = 1:length(A) @@ -755,7 +755,7 @@ function rand!(r::BigRNG, A::Array{BigFloat}) end A end -rand!(A::Array{BigFloat}) = rand!(Base.GMP.DEFAULT_BIGRNG, A) +rand!(A::Array{BigFloat}) = rand!(Base.Random.DEFAULT_BIGRNG, A) function randn(::Type{BigFloat}, randstate::BigRNG) z = BigFloat() @@ -764,7 +764,7 @@ function randn(::Type{BigFloat}, randstate::BigRNG) &z, C_NULL, &randstate, ROUNDING_MODE[end]) z end -randn(::Type{BigFloat}) = randn(BigFloat, Base.GMP.DEFAULT_BIGRNG) +randn(::Type{BigFloat}) = randn(BigFloat, Base.Random.DEFAULT_BIGRNG) randn(::Type{BigFloat}, dims::Dims) = randn!(Array(BigFloat, dims)) randn(::Type{BigFloat}, dims::Int...) = randn!(Array(BigFloat, dims...)) @@ -774,6 +774,6 @@ function randn!(r::BigRNG, A::Array{BigFloat}) end A end -randn!(A::Array{BigFloat}) = randn!(Base.GMP.DEFAULT_BIGRNG, A) +randn!(A::Array{BigFloat}) = randn!(Base.Random.DEFAULT_BIGRNG, A) end #module diff --git a/base/random.jl b/base/random.jl index 877f593e60250..902eabe3d5cd9 100644 --- a/base/random.jl +++ b/base/random.jl @@ -75,6 +75,12 @@ end function srand(seed::Vector{Uint32}) global RANDOM_SEED = seed dsfmt_gv_init_by_array(seed) + s = big(0) + for i in Base.Random.RANDOM_SEED + s = s << 32 + i + end + global DEFAULT_BIGRNG = BigRNG(s) + return end srand(n::Integer) = srand(make_seed(n)) diff --git a/test/random.jl b/test/random.jl index 88443e3d59b31..45aa96e3228fd 100644 --- a/test/random.jl +++ b/test/random.jl @@ -17,3 +17,25 @@ if sizeof(Int32) < sizeof(Int) @test typeof(r) == Int32 @test -1 <= r <= typemax(Int32) end + +range = big(typemax(Int128)):(big(typemax(Int128)) + 10000) +@test rand(range) != rand(range) +@test big(typemax(Int128)) <= rand(range) <= big(typemax(Int128)) + 10000 +r = rand(range, 1, 2) +@test size(r) == (1, 2) +@test typeof(r) == Matrix{BigInt} + +srand(0) +r = rand(range) +f = rand(BigFloat) +srand(0) +@test rand(range) == r +@test rand(BigFloat) == f + +@test rand(BigFloat) != rand(BigFloat) +@test 0.0 <= rand(BigFloat) < 1.0 +@test typeof(rand(BigFloat)) == BigFloat + +r = rand(BigFloat, 1, 3) +@test size(r) == (1, 3) +@test typeof(r) == Matrix{BigFloat}