Skip to content

Commit

Permalink
deprecate srand(rng, filename::AbstractString)
Browse files Browse the repository at this point in the history
`srand` should accept a seed as second argument; here `filename`
is only an indirect seed, preventing the use a string object as
a direct seed.
  • Loading branch information
rfourquet committed Apr 12, 2017
1 parent d91a719 commit 48caedf
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 18 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,8 @@ Deprecated or removed
* The zero-argument constructor `MersenneTwister()` has been
deprecated in favor of the explicit `MersenneTwister(0)` ([#16984]).

* The method `srand(rng, filename, n=4)` has been deprecated ([#21359]).

<!--- generated by NEWS-update.jl: -->
[#265]: https://github.com/JuliaLang/julia/issues/265
[#4615]: https://github.com/JuliaLang/julia/issues/4615
Expand Down
6 changes: 6 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,12 @@ end
# PR #16984
@deprecate MersenneTwister() MersenneTwister(0)

# PR #21359
@deprecate srand(r::MersenneTwister, filename::AbstractString, n::Integer=4) srand(r, read!(filename, Array{UInt32}(Int(n))))
@deprecate srand(filename::AbstractString, n::Integer=4) srand(read!(filename, Array{UInt32}(Int(n))))
@deprecate MersenneTwister(filename::AbstractString) srand(MersenneTwister(0), read!(filename, Array{UInt32}(Int(4))))
@deprecate Base.make_seed(filename::AbstractString, n::Integer) read!(filename, Array{UInt32}(Int(n)))

# #19635
for fname in (:ones, :zeros)
@eval @deprecate ($fname)(T::Type, arr) ($fname)(T, size(arr))
Expand Down
19 changes: 4 additions & 15 deletions base/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -213,25 +213,19 @@ function make_seed(n::Integer)
end
end

function make_seed(filename::AbstractString, n::Integer)
read!(filename, Array{UInt32}(Int(n)))
end

## srand()

"""
srand([rng=GLOBAL_RNG], [seed]) -> rng
srand([rng=GLOBAL_RNG], filename, n=4) -> rng
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, a vector of `UInt32` integers
or a filename, in which case the seed is read from a file (`4n` bytes are read from the file,
where `n` is an optional argument). `RandomDevice` does not support seeding.
`MersenneTwister`, the `seed` may be a non-negative integer or a vector of `UInt32` integers.
`RandomDevice` does not support seeding.
"""
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))


function dsfmt_gv_srand()
Expand All @@ -250,11 +244,6 @@ function srand(seed::Union{Integer, Vector{UInt32}})
dsfmt_gv_srand()
end

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

## Global RNG

const GLOBAL_RNG = MersenneTwister(0)
Expand Down
3 changes: 0 additions & 3 deletions test/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -485,11 +485,8 @@ let g = Base.Random.GLOBAL_RNG,
@test srand() === g
@test srand(rand(UInt)) === g
@test srand(rand(UInt32, rand(1:10))) === g
@test srand(@__FILE__) === g
@test srand(@__FILE__, rand(1:10)) === g
@test srand(m) === m
@test srand(m, rand(UInt)) === m
@test srand(m, rand(UInt32, rand(1:10))) === m
@test srand(m, rand(1:10)) === m
@test srand(m, @__FILE__, rand(1:10)) === m
end

0 comments on commit 48caedf

Please sign in to comment.