Skip to content

Commit

Permalink
Rename srand to Random.seed! (#28295)
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Thill authored and ararslan committed Jul 29, 2018
1 parent 3cfc131 commit cf78a31
Show file tree
Hide file tree
Showing 42 changed files with 138 additions and 131 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -1323,6 +1323,8 @@ Deprecated or removed

* `squeeze` is deprecated in favor of `dropdims`.

* `srand` is deprecated in favor of the unexported `Random.seed!` ([#27726]).

* `realmin`/`realmax` are deprecated in favor of `floatmin`/`floatmax` ([#28302]).

Command-line option changes
Expand Down Expand Up @@ -1674,6 +1676,7 @@ Command-line option changes
[#27635]: https://github.com/JuliaLang/julia/issues/27635
[#27641]: https://github.com/JuliaLang/julia/issues/27641
[#27711]: https://github.com/JuliaLang/julia/issues/27711
[#27726]: https://github.com/JuliaLang/julia/issues/27726
[#27746]: https://github.com/JuliaLang/julia/issues/27746
[#27859]: https://github.com/JuliaLang/julia/issues/27859
[#27908]: https://github.com/JuliaLang/julia/issues/27908
2 changes: 1 addition & 1 deletion doc/src/devdocs/subarrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ cartesian, rather than linear, indexing.
Consider making 2d slices of a 3d array:

```@meta
DocTestSetup = :(import Random; Random.srand(1234))
DocTestSetup = :(import Random; Random.seed!(1234))
```
```jldoctest subarray
julia> A = rand(2,3,4);
Expand Down
2 changes: 1 addition & 1 deletion doc/src/manual/parallel-computing.md
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ julia> function g()
end
g (generic function with 1 method)
julia> srand(1); g() # the result for a single thread is 1000
julia> Random.seed!(1); g() # the result for a single thread is 1000
781
```

Expand Down
8 changes: 4 additions & 4 deletions doc/src/manual/performance-tips.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ so all the performance issues discussed previously apply.
A useful tool for measuring performance is the [`@time`](@ref) macro. We here repeat the example
with the global variable above, but this time with the type annotation removed:

```jldoctest; setup = :(using Random; srand(1234)), filter = r"[0-9\.]+ seconds \(.*?\)"
```jldoctest; setup = :(using Random; Random.seed!(1234)), filter = r"[0-9\.]+ seconds \(.*?\)"
julia> x = rand(1000);
julia> function sum_global()
Expand Down Expand Up @@ -94,7 +94,7 @@ If we instead pass `x` as an argument to the function it no longer allocates mem
(the allocation reported below is due to running the `@time` macro in global scope)
and is significantly faster after the first call:

```jldoctest sumarg; setup = :(using Random; srand(1234)), filter = r"[0-9\.]+ seconds \(.*?\)"
```jldoctest sumarg; setup = :(using Random; Random.seed!(1234)), filter = r"[0-9\.]+ seconds \(.*?\)"
julia> x = rand(1000);
julia> function sum_arg(x)
Expand Down Expand Up @@ -583,7 +583,7 @@ to perform a core computation. Where possible, it is a good idea to put these co
in separate functions. For example, the following contrived function returns an array of a randomly-chosen
type:

```jldoctest; setup = :(using Random; srand(1234))
```jldoctest; setup = :(using Random; Random.seed!(1234))
julia> function strange_twos(n)
a = Vector{rand(Bool) ? Int64 : Float64}(undef, n)
for i = 1:n
Expand All @@ -601,7 +601,7 @@ julia> strange_twos(3)

This should be written as:

```jldoctest; setup = :(using Random; srand(1234))
```jldoctest; setup = :(using Random; Random.seed!(1234))
julia> function fill_twos!(a)
for i = eachindex(a)
a[i] = 2
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/test/bidiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ using LinearAlgebra: BlasReal, BlasFloat
include("testutils.jl") # test_approx_eq_modphase

n = 10 #Size of test matrix
srand(1)
Random.seed!(1)

@testset for relty in (Int, Float32, Float64, BigFloat), elty in (relty, Complex{relty})
if relty <: AbstractFloat
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/test/blas.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module TestBLAS
using Test, LinearAlgebra, Random
using LinearAlgebra: BlasReal, BlasComplex

srand(100)
Random.seed!(100)
## BLAS tests - testing the interface code to BLAS routines
@testset for elty in [Float32, Float64, ComplexF32, ComplexF64]
@testset "syr2k!" begin
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/test/bunchkaufman.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ n = 10
n1 = div(n, 2)
n2 = 2*n1

srand(1234321)
Random.seed!(1234321)

areal = randn(n,n)/2
aimg = randn(n,n)/2
Expand Down
4 changes: 2 additions & 2 deletions stdlib/LinearAlgebra/test/cholesky.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ end
n1 = div(n, 2)
n2 = 2*n1

srand(1234321)
Random.seed!(1234321)

areal = randn(n,n)/2
aimg = randn(n,n)/2
Expand Down Expand Up @@ -73,7 +73,7 @@ end

#these tests were failing on 64-bit linux when inside the inner loop
#for eltya = ComplexF32 and eltyb = Int. The E[i,j] had NaN32 elements
#but only with srand(1234321) set before the loops.
#but only with Random.seed!(1234321) set before the loops.
E = abs.(apd - r'*r)
for i=1:n, j=1:n
@test E[i,j] <= (n+1/(1-(n+1)ε)*real(sqrt(apd[i,i]*apd[j,j]))
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/test/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ n = 10
n1 = div(n, 2)
n2 = 2*n1

srand(1234321)
Random.seed!(1234321)

@testset "Matrix condition number" begin
ainit = rand(n,n)
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/test/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using Test, LinearAlgebra, SparseArrays, Random
using LinearAlgebra: mul!, rmul!, lmul!, ldiv!, rdiv!, BlasFloat, BlasComplex, SingularException

n=12 #Size of matrix problem to test
srand(1)
Random.seed!(1)

@testset for relty in (Float32, Float64, BigFloat), elty in (relty, Complex{relty})
dd=convert(Vector{elty}, randn(n))
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/test/eigen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ n = 10
n1 = div(n, 2)
n2 = 2*n1

srand(1234321)
Random.seed!(1234321)

areal = randn(n,n)/2
aimg = randn(n,n)/2
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/test/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Base.isfinite(q::Quaternion) = isfinite(q.s) & isfinite(q.v1) & isfinite(q.v2) &
(/)(q::Quaternion, w::Quaternion) = q * conj(w) * (1.0 / abs2(w))
(\)(q::Quaternion, w::Quaternion) = conj(q) * w * (1.0 / abs2(q))

srand(123)
Random.seed!(123)

n = 5 # should be odd

Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/test/hessenberg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module TestHessenberg
using Test, LinearAlgebra, Random

let n = 10
srand(1234321)
Random.seed!(1234321)

Areal = randn(n,n)/2
Aimg = randn(n,n)/2
Expand Down
8 changes: 4 additions & 4 deletions stdlib/LinearAlgebra/test/lapack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ using LinearAlgebra: BlasInt
@test_throws ArgumentError LinearAlgebra.LAPACK.chktrans('Z')

@testset "syevr" begin
srand(123)
Random.seed!(123)
Ainit = randn(5,5)
@testset for elty in (Float32, Float64, ComplexF32, ComplexF64)
if elty == ComplexF32 || elty == ComplexF64
Expand Down Expand Up @@ -208,7 +208,7 @@ end

@testset "gels" begin
@testset for elty in (Float32, Float64, ComplexF32, ComplexF64)
srand(913)
Random.seed!(913)
A = rand(elty,10,10)
X = rand(elty,10)
B,Y,z = LAPACK.gels!('N',copy(A),copy(X))
Expand Down Expand Up @@ -443,7 +443,7 @@ end

@testset "sysv" begin
@testset for elty in (Float32, Float64, ComplexF32, ComplexF64)
srand(123)
Random.seed!(123)
A = rand(elty,10,10)
A = A + transpose(A) #symmetric!
b = rand(elty,10)
Expand All @@ -456,7 +456,7 @@ end

@testset "hesv" begin
@testset for elty in (ComplexF32, ComplexF64)
srand(935)
Random.seed!(935)
A = rand(elty,10,10)
A = A + A' #hermitian!
b = rand(elty,10)
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/test/lq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ n = 10
n1 = div(n, 2)
n2 = 2*n1

srand(1234321)
Random.seed!(1234321)

areal = randn(n,n)/2
aimg = randn(n,n)/2
Expand Down
4 changes: 2 additions & 2 deletions stdlib/LinearAlgebra/test/lu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ n = 10
n1 = div(n, 2)
n2 = 2*n1

srand(1234321)
Random.seed!(1234321)

areal = randn(n,n)/2
aimg = randn(n,n)/2
Expand Down Expand Up @@ -208,7 +208,7 @@ end
end

@testset "conversion" begin
srand(3)
Random.seed!(3)
a = Tridiagonal(rand(9),rand(10),rand(9))
fa = Array(a)
falu = lu(fa)
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/test/pinv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module TestPinv

using Test, LinearAlgebra, Random

srand(12345)
Random.seed!(12345)

function hilb(T::Type, n::Integer)
a = Matrix{T}(undef, n, n)
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/test/qr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ n = 10
n1 = div(n, 2)
n2 = 2*n1

srand(1234321)
Random.seed!(1234321)

areal = randn(n,n)/2
aimg = randn(n,n)/2
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/test/schur.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ n = 10
n1 = div(n, 2)
n2 = 2*n1

srand(1234321)
Random.seed!(1234321)

areal = randn(n,n)/2
aimg = randn(n,n)/2
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/test/special.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using Test, LinearAlgebra, SparseArrays, Random
using LinearAlgebra: rmul!

n= 10 #Size of matrix to test
srand(1)
Random.seed!(1)

@testset "Interconversion between special matrix types" begin
a = [1.0:n;]
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/test/svd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ n = 10
n1 = div(n, 2)
n2 = 2*n1

srand(1234321)
Random.seed!(1234321)

areal = randn(n,n)/2
aimg = randn(n,n)/2
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/test/symmetric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module TestSymmetric

using Test, LinearAlgebra, SparseArrays, Random

srand(101)
Random.seed!(101)

@testset "Pauli σ-matrices: " for σ in map(Hermitian,
Any[ [1 0; 0 1], [0 1; 1 0], [0 -im; im 0], [1 0; 0 -1] ])
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/test/triangular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ using LinearAlgebra: BlasFloat, errorbounds, full!, naivesub!, transpose!,
debug && println("Triangular matrices")

n = 9
srand(123)
Random.seed!(123)

debug && println("Test basic type functionality")
@test_throws DimensionMismatch LowerTriangular(randn(5, 4))
Expand Down
4 changes: 2 additions & 2 deletions stdlib/LinearAlgebra/test/tridiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ end

@testset for elty in (Float32, Float64, ComplexF32, ComplexF64, Int)
n = 12 #Size of matrix problem to test
srand(123)
Random.seed!(123)
if elty == Int
srand(61516384)
Random.seed!(61516384)
d = rand(1:100, n)
dl = -rand(0:10, n-1)
du = -rand(0:10, n-1)
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/test/uniformscaling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module TestUniformscaling

using Test, LinearAlgebra, Random, SparseArrays

srand(123)
Random.seed!(123)

@testset "basic functions" begin
@test I[1,1] == 1 # getindex
Expand Down
2 changes: 1 addition & 1 deletion stdlib/Pkg/test/NastyGenerator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function generate_nasty(n::Int, # size of planted solutions
@assert m n
d m-1 || @warn "d=$d, should be ≤ m-1=$(m-1)"

srand(seed)
Random.seed!(seed)

allvers = [sort(unique(randvers(k) for j = 1:q)) for i = 1:m]

Expand Down
2 changes: 1 addition & 1 deletion stdlib/Random/docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Random.shuffle!
## Generators (creation and seeding)

```@docs
Random.srand
Random.seed!
Random.MersenneTwister
Random.RandomDevice
```
Expand Down
18 changes: 9 additions & 9 deletions stdlib/Random/src/RNGs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ The entropy is obtained from the operating system.
RandomDevice

RandomDevice(::Nothing) = RandomDevice()
srand(rng::RandomDevice) = rng
seed!(rng::RandomDevice) = rng


## MersenneTwister
Expand Down Expand Up @@ -110,7 +110,7 @@ of random numbers.
The `seed` may be a non-negative integer or a vector of
`UInt32` integers. If no seed is provided, a randomly generated one
is created (using entropy from the system).
See the [`srand`](@ref) function for reseeding an already existing
See the [`seed!`](@ref) function for reseeding an already existing
`MersenneTwister` object.
Expand All @@ -135,7 +135,7 @@ true
```
"""
MersenneTwister(seed=nothing) =
srand(MersenneTwister(Vector{UInt32}(), DSFMT_state()), seed)
seed!(MersenneTwister(Vector{UInt32}(), DSFMT_state()), seed)


function copy!(dst::MersenneTwister, src::MersenneTwister)
Expand Down Expand Up @@ -274,9 +274,9 @@ function make_seed(n::Integer)
end
end

#### srand()
#### seed!()

function srand(r::MersenneTwister, seed::Vector{UInt32})
function seed!(r::MersenneTwister, seed::Vector{UInt32})
copyto!(resize!(r.seed, length(seed)), seed)
dsfmt_init_by_array(r.state, r.seed)
mt_setempty!(r)
Expand All @@ -285,12 +285,12 @@ function srand(r::MersenneTwister, seed::Vector{UInt32})
return r
end

srand(r::MersenneTwister=GLOBAL_RNG) = srand(r, make_seed())
srand(r::MersenneTwister, n::Integer) = srand(r, make_seed(n))
srand(seed::Union{Integer,Vector{UInt32}}) = srand(GLOBAL_RNG, seed)
seed!(r::MersenneTwister=GLOBAL_RNG) = seed!(r, make_seed())
seed!(r::MersenneTwister, n::Integer) = seed!(r, make_seed(n))
seed!(seed::Union{Integer,Vector{UInt32}}) = seed!(GLOBAL_RNG, seed)


### Global RNG (must be defined after srand)
### Global RNG (must be defined after seed!)

const GLOBAL_RNG = MersenneTwister(0)

Expand Down
Loading

1 comment on commit cf78a31

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executing the daily benchmark build, I will reply here when finished:

@nanosoldier runbenchmarks(ALL, isdaily = true)

Please sign in to comment.