Skip to content

Commit

Permalink
srand() now seeds both RNGs
Browse files Browse the repository at this point in the history
  • Loading branch information
andrioni committed Nov 19, 2013
1 parent 7ef6733 commit bf8c452
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 15 deletions.
1 change: 0 additions & 1 deletion base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
11 changes: 1 addition & 10 deletions base/gmp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand All @@ -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
8 changes: 4 additions & 4 deletions base/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -747,15 +747,15 @@ 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)
A[i] = rand(BigFloat, r)
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()
Expand All @@ -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...))

Expand All @@ -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
6 changes: 6 additions & 0 deletions base/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
22 changes: 22 additions & 0 deletions test/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}

0 comments on commit bf8c452

Please sign in to comment.