From 2a9ce4e858e9947736de87f0242ab22eda281abc Mon Sep 17 00:00:00 2001 From: Rafael Fourquet Date: Fri, 6 Jul 2018 19:16:52 +0200 Subject: [PATCH] Random: deprecate randjump in favor of Future.randjump and accumulate --- NEWS.md | 3 +++ stdlib/Random/src/RNGs.jl | 25 +------------------------ stdlib/Random/src/deprecated.jl | 19 +++++++++++++++++-- stdlib/Random/test/runtests.jl | 20 ++++++++++++++------ 4 files changed, 35 insertions(+), 32 deletions(-) diff --git a/NEWS.md b/NEWS.md index a4336b90d22770..403c8e566725be 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1228,6 +1228,9 @@ Deprecated or removed * `rand(t::Tuple{Vararg{Int}})` is deprecated in favor of `rand(Float64, t)` or `rand(t...)`; `rand(::Tuple)` will have another meaning in the future ([#25429], [#25278]). + * `randjump`, which produced an array, is deprecated in favor of the + scalar version `Future.randjump` used with `accumulate` ([#27746]). + * The `assert` function (and `@assert` macro) have been documented that they are not guaranteed to run under various optimization levels and should therefore not be used to e.g. verify passwords. * `ObjectIdDict` has been deprecated in favor of `IdDict{Any,Any}` ([#25210]). diff --git a/stdlib/Random/src/RNGs.jl b/stdlib/Random/src/RNGs.jl index 3e54b56082e9a5..cd156a942161dd 100644 --- a/stdlib/Random/src/RNGs.jl +++ b/stdlib/Random/src/RNGs.jl @@ -555,30 +555,7 @@ end ### randjump -""" - randjump(r::MersenneTwister, steps::Integer, len::Integer) -> Vector{MersenneTwister} - -Create an array of size `len` of initialized `MersenneTwister` RNG objects. The -first RNG object given as a parameter and following `MersenneTwister` RNGs in the array are -initialized such that a state of the RNG object in the array would be moved forward (without -generating numbers) from a previous RNG object array element by `steps` steps. -One such step corresponds to the generation of two `Float64` numbers. -For each different value of `steps`, a large polynomial has to be generated internally. -One is already pre-computed for `steps=big(10)^20`. -""" -randjump(r::MersenneTwister, steps::Integer, len::Integer) = - _randjump(r, DSFMT.calc_jump(steps), len) - +# Old randjump methods are deprecated, the scalar version is in the Future module. _randjump(r::MersenneTwister, jumppoly::DSFMT.GF2X) = fillcache_zeros!(MersenneTwister(copy(r.seed), DSFMT.dsfmt_jump(r.state, jumppoly))) - -function _randjump(mt::MersenneTwister, jumppoly::DSFMT.GF2X, len::Integer) - mts = MersenneTwister[] - push!(mts, mt) - for i in 1:len-1 - cmt = mts[end] - push!(mts, _randjump(cmt, jumppoly)) - end - return mts -end diff --git a/stdlib/Random/src/deprecated.jl b/stdlib/Random/src/deprecated.jl index 55cfd2d965b5dc..bce29665d2c7e4 100644 --- a/stdlib/Random/src/deprecated.jl +++ b/stdlib/Random/src/deprecated.jl @@ -10,12 +10,27 @@ Base.@deprecate_binding dSFMT DSFMT @deprecate MersenneTwister(filename::AbstractString) srand(MersenneTwister(0), read!(filename, Vector{UInt32}(undef, Int(4)))) function randjump(mt::MersenneTwister, jumps::Integer, jumppoly::AbstractString) - depwarn("`randjump(rng, jumps, jumppoly::AbstractString)` is deprecated; use `randjump(rng, steps, jumps)` instead", :randjump) - Base.Random._randjump(mt, DSFMT.GF2X(jumppoly), jumps) + Base.depwarn("`randjump(rng, jumps, [jumppoly::AbstractString])` is deprecated; use `Future.randjump` and `accumulate` instead", :randjump) + _randjump(mt, DSFMT.GF2X(jumppoly), jumps) end @deprecate randjump(mt::MersenneTwister, jumps::Integer) randjump(mt, big(10)^20, jumps) +function randjump(r::MersenneTwister, steps::Integer, len::Integer) + Base.depwarn("`randjump(rng, steps::Integer, len::Integer` is deprecated; use `Future.randjump` and `accumulate` instead", :randjump) + _randjump(r, DSFMT.calc_jump(steps), len) +end + +function _randjump(mt::MersenneTwister, jumppoly::DSFMT.GF2X, len::Integer) + mts = MersenneTwister[] + push!(mts, mt) + for i in 1:len-1 + cmt = mts[end] + push!(mts, _randjump(cmt, jumppoly)) + end + return mts +end + # PR #25429 @deprecate rand(r::AbstractRNG, dims::Dims) rand(r, Float64, dims) @deprecate rand( dims::Dims) rand(Float64, dims) diff --git a/stdlib/Random/test/runtests.jl b/stdlib/Random/test/runtests.jl index fe0224f018336e..bba70b8aba93a2 100644 --- a/stdlib/Random/test/runtests.jl +++ b/stdlib/Random/test/runtests.jl @@ -11,6 +11,8 @@ using Random.DSFMT using Random: Sampler, SamplerRangeFast, SamplerRangeInt, MT_CACHE_F, MT_CACHE_I +import Future # randjump + @testset "Issue #6573" begin srand(0) rand() @@ -514,7 +516,13 @@ end # test PRNG jump - mts = randjump(mta, 25000, size) + function randjumpvec(m, steps, len) # old version of randjump + mts = accumulate(Future.randjump, m, fill(steps, len-1)) + pushfirst!(mts, m) + mts + end + + mts = randjumpvec(mta, 25000, size) @test length(mts) == 4 for x in (rand(mts[k], Float64) for j=1:step, k=1:size) @@ -523,7 +531,7 @@ end @testset "generated RNGs are in a deterministic state (relatively to ==)" begin m = MersenneTwister() - @test randjump(m, 25000, 2) == randjump(m, 25000, 2) + @test Future.randjump(m, 25000) == Future.randjump(m, 25000) end end @@ -573,10 +581,10 @@ let seed = rand(UInt32, 10) r = MersenneTwister(seed) @test r.seed == seed && r.seed !== seed # RNGs do not share their seed in randjump - let rs = randjump(r, big(10)^20, 2) - @test rs[1].seed !== rs[2].seed - srand(rs[2]) - @test seed == rs[1].seed != rs[2].seed + let r2 = Future.randjump(r, big(10)^20) + @test r.seed !== r2.seed + srand(r2) + @test seed == r.seed != r2.seed end resize!(seed, 4) @test r.seed != seed