diff --git a/base/random.jl b/base/random.jl index 1267f58d53717..b8e8c90d0a220 100644 --- a/base/random.jl +++ b/base/random.jl @@ -126,7 +126,7 @@ end @inline rand_ui2x52_raw(r::MersenneTwister) = rand_ui52_raw(r) % UInt128 << 64 | rand_ui52_raw(r) function srand(r::MersenneTwister, seed::Vector{UInt32}) - r.seed = seed + copy!(resize!(r.seed, length(seed)), seed) dsfmt_init_by_array(r.state, r.seed) mt_setempty!(r) return r diff --git a/test/random.jl b/test/random.jl index 9b0a601a9aafc..1e824f98e9905 100644 --- a/test/random.jl +++ b/test/random.jl @@ -430,9 +430,17 @@ let seed = rand(UInt32, 10) @test s == r end -# intialization +# MersenneTwister initialization with invalid values @test_throws DomainError Base.dSFMT.DSFMT_state(zeros(Int32, rand(0:Base.dSFMT.JN32-1))) @test_throws DomainError MersenneTwister(zeros(UInt32, 1), Base.dSFMT.DSFMT_state(), zeros(Float64, 10), 0) @test_throws DomainError MersenneTwister(zeros(UInt32, 1), Base.dSFMT.DSFMT_state(), zeros(Float64, Base.Random.MTCacheLength), -1) + +# seed is private to MersenneTwister +let seed = rand(UInt32, 10) + r = MersenneTwister(seed) + @test r.seed == seed && r.seed !== seed + resize!(seed, 4) + @test r.seed != seed +end