Skip to content
This repository was archived by the owner on Apr 12, 2019. It is now read-only.

Commit cf78a31

Browse files
Olivier Thillararslan
Olivier Thill
authored andcommitted
Rename srand to Random.seed! (JuliaLang#28295)
1 parent 3cfc131 commit cf78a31

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+138
-131
lines changed

NEWS.md

+3
Original file line numberDiff line numberDiff line change
@@ -1323,6 +1323,8 @@ Deprecated or removed
13231323

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

1326+
* `srand` is deprecated in favor of the unexported `Random.seed!` ([#27726]).
1327+
13261328
* `realmin`/`realmax` are deprecated in favor of `floatmin`/`floatmax` ([#28302]).
13271329

13281330
Command-line option changes
@@ -1674,6 +1676,7 @@ Command-line option changes
16741676
[#27635]: https://github.com/JuliaLang/julia/issues/27635
16751677
[#27641]: https://github.com/JuliaLang/julia/issues/27641
16761678
[#27711]: https://github.com/JuliaLang/julia/issues/27711
1679+
[#27726]: https://github.com/JuliaLang/julia/issues/27726
16771680
[#27746]: https://github.com/JuliaLang/julia/issues/27746
16781681
[#27859]: https://github.com/JuliaLang/julia/issues/27859
16791682
[#27908]: https://github.com/JuliaLang/julia/issues/27908

doc/src/devdocs/subarrays.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ cartesian, rather than linear, indexing.
3737
Consider making 2d slices of a 3d array:
3838

3939
```@meta
40-
DocTestSetup = :(import Random; Random.srand(1234))
40+
DocTestSetup = :(import Random; Random.seed!(1234))
4141
```
4242
```jldoctest subarray
4343
julia> A = rand(2,3,4);

doc/src/manual/parallel-computing.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ julia> function g()
418418
end
419419
g (generic function with 1 method)
420420
421-
julia> srand(1); g() # the result for a single thread is 1000
421+
julia> Random.seed!(1); g() # the result for a single thread is 1000
422422
781
423423
```
424424

doc/src/manual/performance-tips.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ so all the performance issues discussed previously apply.
5858
A useful tool for measuring performance is the [`@time`](@ref) macro. We here repeat the example
5959
with the global variable above, but this time with the type annotation removed:
6060

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

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

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

602602
This should be written as:
603603

604-
```jldoctest; setup = :(using Random; srand(1234))
604+
```jldoctest; setup = :(using Random; Random.seed!(1234))
605605
julia> function fill_twos!(a)
606606
for i = eachindex(a)
607607
a[i] = 2

stdlib/LinearAlgebra/test/bidiag.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ using LinearAlgebra: BlasReal, BlasFloat
88
include("testutils.jl") # test_approx_eq_modphase
99

1010
n = 10 #Size of test matrix
11-
srand(1)
11+
Random.seed!(1)
1212

1313
@testset for relty in (Int, Float32, Float64, BigFloat), elty in (relty, Complex{relty})
1414
if relty <: AbstractFloat

stdlib/LinearAlgebra/test/blas.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module TestBLAS
55
using Test, LinearAlgebra, Random
66
using LinearAlgebra: BlasReal, BlasComplex
77

8-
srand(100)
8+
Random.seed!(100)
99
## BLAS tests - testing the interface code to BLAS routines
1010
@testset for elty in [Float32, Float64, ComplexF32, ComplexF64]
1111
@testset "syr2k!" begin

stdlib/LinearAlgebra/test/bunchkaufman.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ n = 10
1212
n1 = div(n, 2)
1313
n2 = 2*n1
1414

15-
srand(1234321)
15+
Random.seed!(1234321)
1616

1717
areal = randn(n,n)/2
1818
aimg = randn(n,n)/2

stdlib/LinearAlgebra/test/cholesky.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ end
3939
n1 = div(n, 2)
4040
n2 = 2*n1
4141

42-
srand(1234321)
42+
Random.seed!(1234321)
4343

4444
areal = randn(n,n)/2
4545
aimg = randn(n,n)/2
@@ -73,7 +73,7 @@ end
7373

7474
#these tests were failing on 64-bit linux when inside the inner loop
7575
#for eltya = ComplexF32 and eltyb = Int. The E[i,j] had NaN32 elements
76-
#but only with srand(1234321) set before the loops.
76+
#but only with Random.seed!(1234321) set before the loops.
7777
E = abs.(apd - r'*r)
7878
for i=1:n, j=1:n
7979
@test E[i,j] <= (n+1/(1-(n+1)ε)*real(sqrt(apd[i,i]*apd[j,j]))

stdlib/LinearAlgebra/test/dense.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ n = 10
1515
n1 = div(n, 2)
1616
n2 = 2*n1
1717

18-
srand(1234321)
18+
Random.seed!(1234321)
1919

2020
@testset "Matrix condition number" begin
2121
ainit = rand(n,n)

stdlib/LinearAlgebra/test/diagonal.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using Test, LinearAlgebra, SparseArrays, Random
66
using LinearAlgebra: mul!, rmul!, lmul!, ldiv!, rdiv!, BlasFloat, BlasComplex, SingularException
77

88
n=12 #Size of matrix problem to test
9-
srand(1)
9+
Random.seed!(1)
1010

1111
@testset for relty in (Float32, Float64, BigFloat), elty in (relty, Complex{relty})
1212
dd=convert(Vector{elty}, randn(n))

stdlib/LinearAlgebra/test/eigen.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ n = 10
1111
n1 = div(n, 2)
1212
n2 = 2*n1
1313

14-
srand(1234321)
14+
Random.seed!(1234321)
1515

1616
areal = randn(n,n)/2
1717
aimg = randn(n,n)/2

stdlib/LinearAlgebra/test/generic.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Base.isfinite(q::Quaternion) = isfinite(q.s) & isfinite(q.v1) & isfinite(q.v2) &
3131
(/)(q::Quaternion, w::Quaternion) = q * conj(w) * (1.0 / abs2(w))
3232
(\)(q::Quaternion, w::Quaternion) = conj(q) * w * (1.0 / abs2(q))
3333

34-
srand(123)
34+
Random.seed!(123)
3535

3636
n = 5 # should be odd
3737

stdlib/LinearAlgebra/test/hessenberg.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module TestHessenberg
55
using Test, LinearAlgebra, Random
66

77
let n = 10
8-
srand(1234321)
8+
Random.seed!(1234321)
99

1010
Areal = randn(n,n)/2
1111
Aimg = randn(n,n)/2

stdlib/LinearAlgebra/test/lapack.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ using LinearAlgebra: BlasInt
1111
@test_throws ArgumentError LinearAlgebra.LAPACK.chktrans('Z')
1212

1313
@testset "syevr" begin
14-
srand(123)
14+
Random.seed!(123)
1515
Ainit = randn(5,5)
1616
@testset for elty in (Float32, Float64, ComplexF32, ComplexF64)
1717
if elty == ComplexF32 || elty == ComplexF64
@@ -208,7 +208,7 @@ end
208208

209209
@testset "gels" begin
210210
@testset for elty in (Float32, Float64, ComplexF32, ComplexF64)
211-
srand(913)
211+
Random.seed!(913)
212212
A = rand(elty,10,10)
213213
X = rand(elty,10)
214214
B,Y,z = LAPACK.gels!('N',copy(A),copy(X))
@@ -443,7 +443,7 @@ end
443443

444444
@testset "sysv" begin
445445
@testset for elty in (Float32, Float64, ComplexF32, ComplexF64)
446-
srand(123)
446+
Random.seed!(123)
447447
A = rand(elty,10,10)
448448
A = A + transpose(A) #symmetric!
449449
b = rand(elty,10)
@@ -456,7 +456,7 @@ end
456456

457457
@testset "hesv" begin
458458
@testset for elty in (ComplexF32, ComplexF64)
459-
srand(935)
459+
Random.seed!(935)
460460
A = rand(elty,10,10)
461461
A = A + A' #hermitian!
462462
b = rand(elty,10)

stdlib/LinearAlgebra/test/lq.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ n = 10
1111
n1 = div(n, 2)
1212
n2 = 2*n1
1313

14-
srand(1234321)
14+
Random.seed!(1234321)
1515

1616
areal = randn(n,n)/2
1717
aimg = randn(n,n)/2

stdlib/LinearAlgebra/test/lu.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ n = 10
1111
n1 = div(n, 2)
1212
n2 = 2*n1
1313

14-
srand(1234321)
14+
Random.seed!(1234321)
1515

1616
areal = randn(n,n)/2
1717
aimg = randn(n,n)/2
@@ -208,7 +208,7 @@ end
208208
end
209209

210210
@testset "conversion" begin
211-
srand(3)
211+
Random.seed!(3)
212212
a = Tridiagonal(rand(9),rand(10),rand(9))
213213
fa = Array(a)
214214
falu = lu(fa)

stdlib/LinearAlgebra/test/pinv.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module TestPinv
44

55
using Test, LinearAlgebra, Random
66

7-
srand(12345)
7+
Random.seed!(12345)
88

99
function hilb(T::Type, n::Integer)
1010
a = Matrix{T}(undef, n, n)

stdlib/LinearAlgebra/test/qr.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ n = 10
1111
n1 = div(n, 2)
1212
n2 = 2*n1
1313

14-
srand(1234321)
14+
Random.seed!(1234321)
1515

1616
areal = randn(n,n)/2
1717
aimg = randn(n,n)/2

stdlib/LinearAlgebra/test/schur.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ n = 10
1111
n1 = div(n, 2)
1212
n2 = 2*n1
1313

14-
srand(1234321)
14+
Random.seed!(1234321)
1515

1616
areal = randn(n,n)/2
1717
aimg = randn(n,n)/2

stdlib/LinearAlgebra/test/special.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using Test, LinearAlgebra, SparseArrays, Random
66
using LinearAlgebra: rmul!
77

88
n= 10 #Size of matrix to test
9-
srand(1)
9+
Random.seed!(1)
1010

1111
@testset "Interconversion between special matrix types" begin
1212
a = [1.0:n;]

stdlib/LinearAlgebra/test/svd.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ n = 10
3737
n1 = div(n, 2)
3838
n2 = 2*n1
3939

40-
srand(1234321)
40+
Random.seed!(1234321)
4141

4242
areal = randn(n,n)/2
4343
aimg = randn(n,n)/2

stdlib/LinearAlgebra/test/symmetric.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module TestSymmetric
44

55
using Test, LinearAlgebra, SparseArrays, Random
66

7-
srand(101)
7+
Random.seed!(101)
88

99
@testset "Pauli σ-matrices: " for σ in map(Hermitian,
1010
Any[ [1 0; 0 1], [0 1; 1 0], [0 -im; im 0], [1 0; 0 -1] ])

stdlib/LinearAlgebra/test/triangular.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ using LinearAlgebra: BlasFloat, errorbounds, full!, naivesub!, transpose!,
1111
debug && println("Triangular matrices")
1212

1313
n = 9
14-
srand(123)
14+
Random.seed!(123)
1515

1616
debug && println("Test basic type functionality")
1717
@test_throws DimensionMismatch LowerTriangular(randn(5, 4))

stdlib/LinearAlgebra/test/tridiag.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ end
2222

2323
@testset for elty in (Float32, Float64, ComplexF32, ComplexF64, Int)
2424
n = 12 #Size of matrix problem to test
25-
srand(123)
25+
Random.seed!(123)
2626
if elty == Int
27-
srand(61516384)
27+
Random.seed!(61516384)
2828
d = rand(1:100, n)
2929
dl = -rand(0:10, n-1)
3030
du = -rand(0:10, n-1)

stdlib/LinearAlgebra/test/uniformscaling.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module TestUniformscaling
44

55
using Test, LinearAlgebra, Random, SparseArrays
66

7-
srand(123)
7+
Random.seed!(123)
88

99
@testset "basic functions" begin
1010
@test I[1,1] == 1 # getindex

stdlib/Pkg/test/NastyGenerator.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function generate_nasty(n::Int, # size of planted solutions
4040
@assert m n
4141
d m-1 || @warn "d=$d, should be ≤ m-1=$(m-1)"
4242

43-
srand(seed)
43+
Random.seed!(seed)
4444

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

stdlib/Random/docs/src/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Random.shuffle!
5656
## Generators (creation and seeding)
5757

5858
```@docs
59-
Random.srand
59+
Random.seed!
6060
Random.MersenneTwister
6161
Random.RandomDevice
6262
```

stdlib/Random/src/RNGs.jl

+9-9
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ The entropy is obtained from the operating system.
6565
RandomDevice
6666

6767
RandomDevice(::Nothing) = RandomDevice()
68-
srand(rng::RandomDevice) = rng
68+
seed!(rng::RandomDevice) = rng
6969

7070

7171
## MersenneTwister
@@ -110,7 +110,7 @@ of random numbers.
110110
The `seed` may be a non-negative integer or a vector of
111111
`UInt32` integers. If no seed is provided, a randomly generated one
112112
is created (using entropy from the system).
113-
See the [`srand`](@ref) function for reseeding an already existing
113+
See the [`seed!`](@ref) function for reseeding an already existing
114114
`MersenneTwister` object.
115115
116116
@@ -135,7 +135,7 @@ true
135135
```
136136
"""
137137
MersenneTwister(seed=nothing) =
138-
srand(MersenneTwister(Vector{UInt32}(), DSFMT_state()), seed)
138+
seed!(MersenneTwister(Vector{UInt32}(), DSFMT_state()), seed)
139139

140140

141141
function copy!(dst::MersenneTwister, src::MersenneTwister)
@@ -274,9 +274,9 @@ function make_seed(n::Integer)
274274
end
275275
end
276276

277-
#### srand()
277+
#### seed!()
278278

279-
function srand(r::MersenneTwister, seed::Vector{UInt32})
279+
function seed!(r::MersenneTwister, seed::Vector{UInt32})
280280
copyto!(resize!(r.seed, length(seed)), seed)
281281
dsfmt_init_by_array(r.state, r.seed)
282282
mt_setempty!(r)
@@ -285,12 +285,12 @@ function srand(r::MersenneTwister, seed::Vector{UInt32})
285285
return r
286286
end
287287

288-
srand(r::MersenneTwister=GLOBAL_RNG) = srand(r, make_seed())
289-
srand(r::MersenneTwister, n::Integer) = srand(r, make_seed(n))
290-
srand(seed::Union{Integer,Vector{UInt32}}) = srand(GLOBAL_RNG, seed)
288+
seed!(r::MersenneTwister=GLOBAL_RNG) = seed!(r, make_seed())
289+
seed!(r::MersenneTwister, n::Integer) = seed!(r, make_seed(n))
290+
seed!(seed::Union{Integer,Vector{UInt32}}) = seed!(GLOBAL_RNG, seed)
291291

292292

293-
### Global RNG (must be defined after srand)
293+
### Global RNG (must be defined after seed!)
294294

295295
const GLOBAL_RNG = MersenneTwister(0)
296296

0 commit comments

Comments
 (0)