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 May 3, 2017
1 parent 0155f51 commit 93e8eb1
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 18 deletions.
31 changes: 31 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
Julia v0.7.0 Release Notes
==========================

New language features
---------------------


Language changes
----------------


Breaking changes
----------------

This section lists changes that do not have deprecation warnings.


Library improvements
--------------------


Compiler/Runtime improvements
-----------------------------


Deprecated or removed
---------------------

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


Julia v0.6.0 Release Notes
==========================

Expand Down
9 changes: 9 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1324,5 +1324,14 @@ end

# END 0.6 deprecations

# BEGIN 0.7 deprecations

# 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))))

# END 0.7 deprecations

# BEGIN 1.0 deprecations
# END 1.0 deprecations
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, Vector{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 93e8eb1

Please sign in to comment.