Skip to content

Commit

Permalink
Merge pull request #9125 from JuliaLang/rf/srand-librmath
Browse files Browse the repository at this point in the history
fix #9124 (srand initialize libRmath RNG)
  • Loading branch information
ViralBShah committed Nov 24, 2014
2 parents c1005a2 + a66bcfc commit 3ccaf3c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
9 changes: 8 additions & 1 deletion base/dSFMT.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module dSFMT

export DSFMT_state, dsfmt_get_min_array_size, dsfmt_get_idstring,
dsfmt_init_gen_rand, dsfmt_init_by_array,
dsfmt_init_gen_rand, dsfmt_init_by_array, dsfmt_gv_init_by_array,
dsfmt_fill_array_close_open!, dsfmt_fill_array_close1_open2!,
win32_SystemFunction036!

Expand Down Expand Up @@ -39,6 +39,13 @@ function dsfmt_init_by_array(s::DSFMT_state, seed::Vector{UInt32})
s.val, seed, length(seed))
end

function dsfmt_gv_init_by_array(seed::Vector{UInt32})
ccall((:dsfmt_gv_init_by_array,:libdSFMT),
Void,
(Ptr{UInt32}, Int32),
seed, length(seed))
end

function dsfmt_fill_array_close1_open2!(s::DSFMT_state, A::Ptr{Float64}, n::Int)
@assert Int(A) % 16 == 0 # the underlying C array must be 16-byte aligned
@assert dsfmt_min_array_size <= n && iseven(n)
Expand Down
35 changes: 23 additions & 12 deletions base/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,11 @@ function srand(r::MersenneTwister, seed::Vector{UInt32})
return r
end

## initialization

function __init__()
srand()
## initialization

# Temporary fix for #8874
ccall((:dsfmt_gv_init_by_array,:libdSFMT),
Void,
(Ptr{UInt32}, Int32),
1+GLOBAL_RNG.seed, length(GLOBAL_RNG.seed))
__init__() = srand()

end

## make_seed()
# make_seed methods produce values of type Array{UInt32}, suitable for MersenneTwister seeding
Expand Down Expand Up @@ -130,9 +123,27 @@ srand(r::MersenneTwister) = srand(r, make_seed())
srand(r::MersenneTwister, n::Integer) = srand(r, make_seed(n))
srand(r::MersenneTwister, filename::AbstractString, n::Integer=4) = srand(r, make_seed(filename, n))

srand() = srand(GLOBAL_RNG)
srand(seed::Union(Integer, Vector{UInt32})) = srand(GLOBAL_RNG, seed)
srand(filename::AbstractString, n::Integer=4) = srand(GLOBAL_RNG, filename, n)

function dsfmt_gv_srand()
# Temporary fix for #8874 and #9124: update global RNG for Rmath
dsfmt_gv_init_by_array(GLOBAL_RNG.seed+1)
return GLOBAL_RNG
end

function srand()
srand(GLOBAL_RNG)
dsfmt_gv_srand()
end

function srand(seed::Union(Integer, Vector{UInt32}))
srand(GLOBAL_RNG, seed)
dsfmt_gv_srand()
end

function srand(filename::AbstractString, n::Integer=4)
srand(GLOBAL_RNG, filename, n)
dsfmt_gv_srand()
end

## Global RNG

Expand Down

0 comments on commit 3ccaf3c

Please sign in to comment.