Skip to content

Commit

Permalink
re-enable the 0-arg MersenneTwister() constructor
Browse files Browse the repository at this point in the history
The arguments to `srand(rng, ...)` and `typeof(rng)(...)`
should mirror each-other, in particular
`srand(MersenneTwister(0))` and `MersenneTwister()` should
produce an equivalent object.
Also, `srand(::RandomDevice)` has been added for consistency,
which could be useful in generic code.
  • Loading branch information
rfourquet committed May 16, 2017
1 parent 12bf0bc commit 2a77314
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
3 changes: 0 additions & 3 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1307,9 +1307,6 @@ end
end
end

# PR #16984
@deprecate MersenneTwister() MersenneTwister(0)

# #19635
for fname in (:ones, :zeros)
@eval @deprecate ($fname)(T::Type, arr) ($fname)(T, size(arr))
Expand Down
25 changes: 18 additions & 7 deletions base/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ end # os-test
RandomDevice()
Create a `RandomDevice` RNG object. Two such objects will always generate different streams of random numbers.
The entropy is obtained from the operating system.
"""
RandomDevice

Expand All @@ -85,16 +86,21 @@ mutable struct MersenneTwister <: AbstractRNG
end
end

MersenneTwister(seed::Vector{UInt32}, state::DSFMT_state) =
_MersenneTwister(seed=Vector{UInt32}(), state=DSFMT_state()) =
MersenneTwister(seed, state, zeros(Float64, MTCacheLength), MTCacheLength)

"""
MersenneTwister(seed)
MersenneTwister()
Create a `MersenneTwister` RNG object. Different RNG objects can have their own seeds, which
may be useful for generating different streams of random numbers.
The `seed` may be a non-negative integer or a vector of `UInt32` integers.
If no seed is provided, a random one is selected (using entropy from the system).
See the [`srand`](@ref) function for reseeding an already existing `MersenneTwister` object.
"""
MersenneTwister(seed) = srand(MersenneTwister(Vector{UInt32}(), DSFMT_state()), seed)
MersenneTwister(seed) = srand(_MersenneTwister(), seed)
MersenneTwister() = srand(_MersenneTwister())

function copy!(dst::MersenneTwister, src::MersenneTwister)
copy!(resize!(dst.seed, length(src.seed)), src.seed)
Expand Down Expand Up @@ -166,7 +172,7 @@ function randjump(mt::MersenneTwister, jumps::Integer, jumppoly::AbstractString)
push!(mts, mt)
for i in 1:jumps-1
cmt = mts[end]
push!(mts, MersenneTwister(copy(cmt.seed), dSFMT.dsfmt_jump(cmt.state, jumppoly)))
push!(mts, _MersenneTwister(copy(cmt.seed), dSFMT.dsfmt_jump(cmt.state, jumppoly)))
end
return mts
end
Expand Down Expand Up @@ -219,11 +225,16 @@ end
srand([rng=GLOBAL_RNG], seed) -> rng
srand([rng=GLOBAL_RNG]) -> rng
Reseed the random number generator. If a `seed` is provided, the RNG will give a
reproducible sequence of numbers, otherwise Julia will get entropy from the system. For
`MersenneTwister`, the `seed` may be a non-negative integer or a vector of `UInt32` integers.
`RandomDevice` does not support seeding.
Reseed the random number generator. If a `seed` is provided, the RNG
will give a reproducible sequence of numbers.
After the call to `srand`, `rng` is equivalent to a newly created object
with the same parameters (e.g.
`MersenneTwister(1) == srand(MersenneTwister(), 1)`).
"""
function srand end

srand(r::RandomDevice) = r

srand(r::MersenneTwister) = srand(r, make_seed())
srand(r::MersenneTwister, n::Integer) = srand(r, make_seed(n))

Expand Down
5 changes: 2 additions & 3 deletions test/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ A = zeros(UInt128, 2, 2)
@test_throws BoundsError rand!(MersenneTwister(0), A, 5)

# rand from AbstractArray
let mt = MersenneTwister(0)
srand(mt)
let mt = MersenneTwister()
@test rand(mt, 0:3:1000) in 0:3:1000
@test issubset(rand!(mt, Array{Int}(100), 0:3:1000), 0:3:1000)
coll = Any[2, UInt128(128), big(619), "string"]
Expand Down Expand Up @@ -377,7 +376,7 @@ function hist(X,n)
end

# test uniform distribution of floats
for rng in [srand(MersenneTwister(0)), RandomDevice()]
for rng in [MersenneTwister(), RandomDevice()]
for T in [Float16,Float32,Float64]
# array version
counts = hist(rand(rng, T, 2000), 4)
Expand Down

0 comments on commit 2a77314

Please sign in to comment.