Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make MersenneTwister() randomly seeded #16984

Merged
merged 3 commits into from
Apr 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,9 @@ Deprecated or removed
* `convert` methods from `Diagonal` and `Bidiagonal` to subtypes of
`AbstractTriangular` have been deprecated ([#17723]).

* The zero-argument constructor `MersenneTwister()` has been
deprecated in favor of the explicit `MersenneTwister(0)` ([#16984]).

<!--- generated by NEWS-update.jl: -->
[#265]: https://github.com/JuliaLang/julia/issues/265
[#4615]: https://github.com/JuliaLang/julia/issues/4615
Expand Down
3 changes: 3 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1307,6 +1307,9 @@ 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
6 changes: 3 additions & 3 deletions base/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ MersenneTwister(seed::Vector{UInt32}, state::DSFMT_state) =
MersenneTwister(seed, state, zeros(Float64, MTCacheLength), MTCacheLength)

"""
MersenneTwister(seed=0)
MersenneTwister(seed)

Create a `MersenneTwister` RNG object. Different RNG objects can have their own seeds, which
may be useful for generating different streams of random numbers.
"""
MersenneTwister(seed=0) = srand(MersenneTwister(Vector{UInt32}(), DSFMT_state()), seed)
MersenneTwister(seed) = srand(MersenneTwister(Vector{UInt32}(), DSFMT_state()), seed)

function copy!(dst::MersenneTwister, src::MersenneTwister)
copy!(resize!(dst.seed, length(src.seed)), src.seed)
Expand Down Expand Up @@ -256,7 +256,7 @@ end

## Global RNG

const GLOBAL_RNG = MersenneTwister()
const GLOBAL_RNG = MersenneTwister(0)
globalRNG() = GLOBAL_RNG

# rand: a non-specified RNG defaults to GLOBAL_RNG
Expand Down
11 changes: 5 additions & 6 deletions test/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ srand(0); rand(); x = rand(384)
@test length(randn(4, 5)) == 20
@test length(bitrand(4, 5)) == 20

@test rand(MersenneTwister()) == 0.8236475079774124
@test rand(MersenneTwister(0)) == 0.8236475079774124
@test rand(MersenneTwister(42)) == 0.5331830160438613
# Try a seed larger than 2^32
Expand All @@ -38,7 +37,7 @@ A = zeros(UInt128, 2, 2)
@test_throws BoundsError rand!(MersenneTwister(0), A, 5)

# rand from AbstractArray
let mt = MersenneTwister()
let mt = MersenneTwister(0)
srand(mt)
@test rand(mt, 0:3:1000) in 0:3:1000
@test issubset(rand!(mt, Array{Int}(100), 0:3:1000), 0:3:1000)
Expand Down Expand Up @@ -227,7 +226,7 @@ u4 = uuid4()
@test u4 == UUID(string(u4)) == UUID(GenericString(string(u4)))
@test u1 == UUID(UInt128(u1))
@test u4 == UUID(UInt128(u4))
@test uuid4(MersenneTwister()) == uuid4(MersenneTwister())
@test uuid4(MersenneTwister(0)) == uuid4(MersenneTwister(0))
@test_throws ArgumentError UUID("550e8400e29b-41d4-a716-446655440000")
@test_throws ArgumentError UUID("550e8400e29b-41d4-a716-44665544000098")
@test_throws ArgumentError UUID("z50e8400-e29b-41d4-a716-446655440000")
Expand Down Expand Up @@ -278,7 +277,7 @@ let mt = MersenneTwister(0)
end

# Issue #9037
let mt = MersenneTwister()
let mt = MersenneTwister(0)
a = Array{Float64}(0)
resize!(a, 1000) # could be 8-byte aligned
b = Array{Float64}(1000) # should be 16-byte aligned
Expand Down Expand Up @@ -306,7 +305,7 @@ let a = [rand(RandomDevice(), UInt128) for i=1:10]
end

# test all rand APIs
for rng in ([], [MersenneTwister()], [RandomDevice()])
for rng in ([], [MersenneTwister(0)], [RandomDevice()])
types = [Base.BitInteger_types..., Bool, Float16, Float32, Float64, Char]
ftypes = [Float16, Float32, Float64]
b2 = big(2)
Expand Down Expand Up @@ -378,7 +377,7 @@ function hist(X,n)
end

# test uniform distribution of floats
for rng in [srand(MersenneTwister()), RandomDevice()]
for rng in [srand(MersenneTwister(0)), RandomDevice()]
for T in [Float16,Float32,Float64]
# array version
counts = hist(rand(rng, T, 2000), 4)
Expand Down
2 changes: 1 addition & 1 deletion test/sparse/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ let xr = sprand(Bool, 1000, 0.9)
@test all(nonzeros(xr))
end

let r1 = MersenneTwister(), r2 = MersenneTwister()
let r1 = MersenneTwister(0), r2 = MersenneTwister(0)
@test sprand(r1, 100, .9) == sprand(r2, 100, .9)
@test sprandn(r1, 100, .9) == sprandn(r2, 100, .9)
@test sprand(r1, Bool, 100, .9, ) == sprand(r2, Bool, 100, .9)
Expand Down