From a707aacc5c3bdc059bc8c1ced6e3324d5bcd38b1 Mon Sep 17 00:00:00 2001 From: Olivier Date: Thu, 26 Jul 2018 16:45:22 +0100 Subject: [PATCH 01/16] Changed implementation name of srand -> seed and removed export. --- stdlib/Random/src/RNGs.jl | 18 +++++++++--------- stdlib/Random/src/Random.jl | 23 +++++++++++------------ 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/stdlib/Random/src/RNGs.jl b/stdlib/Random/src/RNGs.jl index bfe357dd4e7b0..4c8dcf15bc482 100644 --- a/stdlib/Random/src/RNGs.jl +++ b/stdlib/Random/src/RNGs.jl @@ -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 @@ -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. @@ -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) @@ -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) @@ -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) diff --git a/stdlib/Random/src/Random.jl b/stdlib/Random/src/Random.jl index e8fae616ad8ea..191858bd54ab4 100644 --- a/stdlib/Random/src/Random.jl +++ b/stdlib/Random/src/Random.jl @@ -17,8 +17,7 @@ using Serialization import Serialization: serialize, deserialize import Base: rand, randn -export srand, - rand!, randn!, +export rand!, randn!, randexp, randexp!, bitrand, randstring, @@ -278,7 +277,7 @@ include("normal.jl") include("misc.jl") include("deprecated.jl") -## rand & rand! & srand docstrings +## rand & rand! & seed docstrings """ rand([rng=GLOBAL_RNG], [S], [dims...]) @@ -342,25 +341,25 @@ julia> rand!(rng, zeros(5)) rand! """ - srand([rng=GLOBAL_RNG], seed) -> rng - srand([rng=GLOBAL_RNG]) -> rng + seed([rng=GLOBAL_RNG], seed) -> rng + seed([rng=GLOBAL_RNG]) -> rng Reseed the random number generator: `rng` will give a reproducible sequence of numbers if and only if a `seed` is provided. Some RNGs don't accept a seed, like `RandomDevice`. -After the call to `srand`, `rng` is equivalent to a newly created +After the call to `seed`, `rng` is equivalent to a newly created object initialized with the same seed. # Examples ```julia-repl -julia> srand(1234); +julia> seed(1234); julia> x1 = rand(2) 2-element Array{Float64,1}: 0.590845 0.766797 -julia> srand(1234); +julia> seed(1234); julia> x2 = rand(2) 2-element Array{Float64,1}: @@ -373,19 +372,19 @@ true julia> rng = MersenneTwister(1234); rand(rng, 2) == x1 true -julia> MersenneTwister(1) == srand(rng, 1) +julia> MersenneTwister(1) == seed(rng, 1) true -julia> rand(srand(rng), Bool) # not reproducible +julia> rand(seed(rng), Bool) # not reproducible true -julia> rand(srand(rng), Bool) +julia> rand(seed(rng), Bool) false julia> rand(MersenneTwister(), Bool) # not reproducible either true ``` """ -srand(rng::AbstractRNG, ::Nothing) = srand(rng) +seed(rng::AbstractRNG, ::Nothing) = seed(rng) end # module From a0f00aaa72f5256b74cf627a3805899fd1ddab35 Mon Sep 17 00:00:00 2001 From: Olivier Date: Thu, 26 Jul 2018 16:53:10 +0100 Subject: [PATCH 02/16] Switched usage in stdlb/Random and added deprecation. --- stdlib/Random/docs/src/index.md | 6 ++++ stdlib/Random/src/deprecated.jl | 3 ++ stdlib/Random/src/misc.jl | 2 +- stdlib/Random/test/runtests.jl | 58 ++++++++++++++++----------------- 4 files changed, 39 insertions(+), 30 deletions(-) diff --git a/stdlib/Random/docs/src/index.md b/stdlib/Random/docs/src/index.md index a26e0bb096c46..9b7f7e47ffe67 100644 --- a/stdlib/Random/docs/src/index.md +++ b/stdlib/Random/docs/src/index.md @@ -30,6 +30,12 @@ Additionally, normal and exponential distributions are implemented for some `Abs ## Random generation functions ```@docs +<<<<<<< a707aacc5c3bdc059bc8c1ced6e3324d5bcd38b1 +======= +Random.seed +Random.MersenneTwister +Random.RandomDevice +>>>>>>> Switched usage in stdlb/Random and added deprecation. Random.rand Random.rand! Random.bitrand diff --git a/stdlib/Random/src/deprecated.jl b/stdlib/Random/src/deprecated.jl index bce29665d2c7e..cc227967f8dc3 100644 --- a/stdlib/Random/src/deprecated.jl +++ b/stdlib/Random/src/deprecated.jl @@ -37,3 +37,6 @@ end # PR #25668 @deprecate RandomDevice(unlimited::Bool) RandomDevice(; unlimited=unlimited) + +# PR #Current +@deprecate srand Random.seed diff --git a/stdlib/Random/src/misc.jl b/stdlib/Random/src/misc.jl index 367163788d516..77b3a7a4d949f 100644 --- a/stdlib/Random/src/misc.jl +++ b/stdlib/Random/src/misc.jl @@ -52,7 +52,7 @@ number generator, see [Random Numbers](@ref). # Examples ```jldoctest -julia> srand(0); randstring() +julia> seed(0); randstring() "0IPrGg0J" julia> randstring(MersenneTwister(0), 'a':'z', 6) diff --git a/stdlib/Random/test/runtests.jl b/stdlib/Random/test/runtests.jl index 859b320ebd99a..eea996bd60717 100644 --- a/stdlib/Random/test/runtests.jl +++ b/stdlib/Random/test/runtests.jl @@ -15,7 +15,7 @@ using Random: Sampler, SamplerRangeFast, SamplerRangeInt, MT_CACHE_F, MT_CACHE_I import Future # randjump @testset "Issue #6573" begin - srand(0) + seed(0) rand() x = rand(384) @test findall(x .== rand()) == [] @@ -131,9 +131,9 @@ for T in [UInt32, UInt64, UInt128, Int128] @test size(r) == (1, 2) @test typeof(r) == Matrix{BigInt} guardsrand() do - srand(0) + seed(0) r = rand(s) - srand(0) + seed(0) @test rand(s) == r end end @@ -225,13 +225,13 @@ randmtzig_fill_ziggurat_tables() #same random numbers on for small ranges on all systems guardsrand() do seed = rand(UInt) - srand(seed) + seed(seed) r = map(Int64, rand(map(Int32, 97:122))) - srand(seed) + seed(seed) @test r == rand(map(Int64, 97:122)) - srand(seed) + seed(seed) r = map(UInt64, rand(map(UInt32, 97:122))) - srand(seed) + seed(seed) @test r == rand(map(UInt64, 97:122)) end @@ -266,7 +266,7 @@ let mt = MersenneTwister(0) 0x4b54632b4619f4eca22675166784d229][i] end - srand(mt, 0) + seed(mt, 0) for (i,T) in enumerate([Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, Float16, Float32]) A = Vector{T}(undef, 16) B = Vector{T}(undef, 31) @@ -278,7 +278,7 @@ let mt = MersenneTwister(0) 61881313582466480231846019869039259750, Float16(0.38672), 0.20027375f0][i] end - srand(mt, 0) + seed(mt, 0) AF64 = Vector{Float64}(undef, Random.dsfmt_get_min_array_size()-1) @test rand!(mt, AF64)[end] == 0.957735065345398 @test rand!(mt, AF64)[end] == 0.6492481059865669 @@ -304,7 +304,7 @@ let mt = MersenneTwister(0) for A in (a, b, c) local A - srand(mt, 0) + seed(mt, 0) rand(mt) # this is to fill mt.vals, cf. #9040 rand!(mt, A) # must not segfault even if Int(pointer(A)) % 16 != 0 @test A[end-4:end] == [0.3371041633752143, 0.41147647589610803, 0.6063082992397912, 0.9103565379264364, 0.16456579813368521] @@ -538,8 +538,8 @@ end # test that the following is not an error (#16925) guardsrand() do - srand(typemax(UInt)) - srand(typemax(UInt128)) + seed(typemax(UInt)) + seed(typemax(UInt128)) end # copy, == and hash @@ -584,24 +584,24 @@ let seed = rand(UInt32, 10) # RNGs do not share their seed in randjump let r2 = Future.randjump(r, big(10)^20) @test r.seed !== r2.seed - srand(r2) + seed(r2) @test seed == r.seed != r2.seed end resize!(seed, 4) @test r.seed != seed end -# srand(rng, ...) returns rng (#21248) +# seed(rng, ...) returns rng (#21248) guardsrand() do g = Random.GLOBAL_RNG m = MersenneTwister(0) - @test srand() === g - @test srand(rand(UInt)) === g - @test srand(rand(UInt32, 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 seed() === g + @test seed(rand(UInt)) === g + @test seed(rand(UInt32, rand(1:10))) === g + @test seed(m) === m + @test seed(m, rand(UInt)) === m + @test seed(m, rand(UInt32, rand(1:10))) === m + @test seed(m, rand(1:10)) === m end # Issue 20062 - ensure internal functions reserve_1, reserve are type-stable @@ -633,7 +633,7 @@ end # this shouldn't crash (#22403) @test_throws ArgumentError rand!(Union{UInt,Int}[1, 2, 3]) -@testset "$RNG() & srand(rng::$RNG) initializes randomly" for RNG in (MersenneTwister, RandomDevice) +@testset "$RNG() & seed(rng::$RNG) initializes randomly" for RNG in (MersenneTwister, RandomDevice) m = RNG() a = rand(m, Int) m = RNG() @@ -642,22 +642,22 @@ end m = RNG(nothing) b = rand(m, Int) @test b != a - srand(m) + seed(m) c = rand(m, Int) @test c ∉ (a, b) - srand(m) + seed(m) @test rand(m, Int) ∉ (a, b, c) - srand(m, nothing) + seed(m, nothing) d = rand(m, Int) @test d ∉ (a, b, c) - srand(m, nothing) + seed(m, nothing) @test rand(m, Int) ∉ (a, b, c, d) end -@testset "MersenneTwister($seed) & srand(m::MersenneTwister, $seed) produce the same stream" for seed in [0:5; 10000:10005] +@testset "MersenneTwister($seed) & seed(m::MersenneTwister, $seed) produce the same stream" for seed in [0:5; 10000:10005] m = MersenneTwister(seed) a = [rand(m) for _=1:100] - srand(m, seed) + seed(m, seed) @test a == [rand(m) for _=1:100] end @@ -669,7 +669,7 @@ end @testset "rand(::$(typeof(RNG)), ::UnitRange{$T}" for RNG ∈ (MersenneTwister(), RandomDevice()), T ∈ (Int32, UInt32, Int64, Int128, UInt128) - RNG isa MersenneTwister && srand(RNG, rand(UInt128)) # for reproducibility + RNG isa MersenneTwister && seed(RNG, rand(UInt128)) # for reproducibility r = T(1):T(108) @test rand(RNG, SamplerRangeFast(r)) ∈ r end From 4a1291f10dfba34a8490e9c4a5b390e2f181a3bd Mon Sep 17 00:00:00 2001 From: Olivier Date: Thu, 26 Jul 2018 17:14:20 +0100 Subject: [PATCH 03/16] Change usage in stdlib. --- stdlib/LinearAlgebra/test/bidiag.jl | 2 +- stdlib/LinearAlgebra/test/blas.jl | 2 +- stdlib/LinearAlgebra/test/bunchkaufman.jl | 2 +- stdlib/LinearAlgebra/test/cholesky.jl | 4 ++-- stdlib/LinearAlgebra/test/dense.jl | 2 +- stdlib/LinearAlgebra/test/diagonal.jl | 2 +- stdlib/LinearAlgebra/test/eigen.jl | 2 +- stdlib/LinearAlgebra/test/generic.jl | 2 +- stdlib/LinearAlgebra/test/hessenberg.jl | 2 +- stdlib/LinearAlgebra/test/lapack.jl | 8 ++++---- stdlib/LinearAlgebra/test/lq.jl | 2 +- stdlib/LinearAlgebra/test/lu.jl | 4 ++-- stdlib/LinearAlgebra/test/pinv.jl | 2 +- stdlib/LinearAlgebra/test/qr.jl | 2 +- stdlib/LinearAlgebra/test/schur.jl | 2 +- stdlib/LinearAlgebra/test/special.jl | 2 +- stdlib/LinearAlgebra/test/svd.jl | 2 +- stdlib/LinearAlgebra/test/symmetric.jl | 2 +- stdlib/LinearAlgebra/test/triangular.jl | 2 +- stdlib/LinearAlgebra/test/tridiag.jl | 4 ++-- stdlib/LinearAlgebra/test/uniformscaling.jl | 2 +- stdlib/Pkg/test/NastyGenerator.jl | 2 +- stdlib/SparseArrays/src/sparsematrix.jl | 4 ++-- stdlib/SparseArrays/test/sparse.jl | 6 +++--- stdlib/SparseArrays/test/sparsevector.jl | 2 +- stdlib/SuiteSparse/test/cholmod.jl | 4 ++-- stdlib/Test/src/Test.jl | 14 +++++++------- stdlib/Test/test/runtests.jl | 10 +++++----- 28 files changed, 48 insertions(+), 48 deletions(-) diff --git a/stdlib/LinearAlgebra/test/bidiag.jl b/stdlib/LinearAlgebra/test/bidiag.jl index f90a13caddaa3..bf4cad2aa4dd3 100644 --- a/stdlib/LinearAlgebra/test/bidiag.jl +++ b/stdlib/LinearAlgebra/test/bidiag.jl @@ -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 diff --git a/stdlib/LinearAlgebra/test/blas.jl b/stdlib/LinearAlgebra/test/blas.jl index d09f35899c358..e3c952324631c 100644 --- a/stdlib/LinearAlgebra/test/blas.jl +++ b/stdlib/LinearAlgebra/test/blas.jl @@ -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 diff --git a/stdlib/LinearAlgebra/test/bunchkaufman.jl b/stdlib/LinearAlgebra/test/bunchkaufman.jl index 5b815b10eb4d7..b985d138e7ac7 100644 --- a/stdlib/LinearAlgebra/test/bunchkaufman.jl +++ b/stdlib/LinearAlgebra/test/bunchkaufman.jl @@ -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 diff --git a/stdlib/LinearAlgebra/test/cholesky.jl b/stdlib/LinearAlgebra/test/cholesky.jl index 4f869dc836ade..488871a108284 100644 --- a/stdlib/LinearAlgebra/test/cholesky.jl +++ b/stdlib/LinearAlgebra/test/cholesky.jl @@ -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 @@ -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])) diff --git a/stdlib/LinearAlgebra/test/dense.jl b/stdlib/LinearAlgebra/test/dense.jl index ed192251a958a..b9803b04b1725 100644 --- a/stdlib/LinearAlgebra/test/dense.jl +++ b/stdlib/LinearAlgebra/test/dense.jl @@ -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) diff --git a/stdlib/LinearAlgebra/test/diagonal.jl b/stdlib/LinearAlgebra/test/diagonal.jl index 028413ef3d318..5d6385150ff43 100644 --- a/stdlib/LinearAlgebra/test/diagonal.jl +++ b/stdlib/LinearAlgebra/test/diagonal.jl @@ -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)) diff --git a/stdlib/LinearAlgebra/test/eigen.jl b/stdlib/LinearAlgebra/test/eigen.jl index 7862ae8d78822..d712802781850 100644 --- a/stdlib/LinearAlgebra/test/eigen.jl +++ b/stdlib/LinearAlgebra/test/eigen.jl @@ -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 diff --git a/stdlib/LinearAlgebra/test/generic.jl b/stdlib/LinearAlgebra/test/generic.jl index 306f1d89d4b02..1a817ef1602a3 100644 --- a/stdlib/LinearAlgebra/test/generic.jl +++ b/stdlib/LinearAlgebra/test/generic.jl @@ -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 diff --git a/stdlib/LinearAlgebra/test/hessenberg.jl b/stdlib/LinearAlgebra/test/hessenberg.jl index 51c85f10edb66..9616ed637b25a 100644 --- a/stdlib/LinearAlgebra/test/hessenberg.jl +++ b/stdlib/LinearAlgebra/test/hessenberg.jl @@ -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 diff --git a/stdlib/LinearAlgebra/test/lapack.jl b/stdlib/LinearAlgebra/test/lapack.jl index 9289a0b3efbc9..6539ffcc4ecce 100644 --- a/stdlib/LinearAlgebra/test/lapack.jl +++ b/stdlib/LinearAlgebra/test/lapack.jl @@ -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 @@ -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)) @@ -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) @@ -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) diff --git a/stdlib/LinearAlgebra/test/lq.jl b/stdlib/LinearAlgebra/test/lq.jl index d9a7f552de63e..dcc4d84eb6716 100644 --- a/stdlib/LinearAlgebra/test/lq.jl +++ b/stdlib/LinearAlgebra/test/lq.jl @@ -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 diff --git a/stdlib/LinearAlgebra/test/lu.jl b/stdlib/LinearAlgebra/test/lu.jl index 7cd747c4c9a21..10ee400223888 100644 --- a/stdlib/LinearAlgebra/test/lu.jl +++ b/stdlib/LinearAlgebra/test/lu.jl @@ -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 @@ -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) diff --git a/stdlib/LinearAlgebra/test/pinv.jl b/stdlib/LinearAlgebra/test/pinv.jl index 489f7c0c8e421..54bf9af3a1986 100644 --- a/stdlib/LinearAlgebra/test/pinv.jl +++ b/stdlib/LinearAlgebra/test/pinv.jl @@ -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) diff --git a/stdlib/LinearAlgebra/test/qr.jl b/stdlib/LinearAlgebra/test/qr.jl index 91106676c52f6..4a721f59f4f77 100644 --- a/stdlib/LinearAlgebra/test/qr.jl +++ b/stdlib/LinearAlgebra/test/qr.jl @@ -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 diff --git a/stdlib/LinearAlgebra/test/schur.jl b/stdlib/LinearAlgebra/test/schur.jl index 488b4b86f15d3..78a23fa080998 100644 --- a/stdlib/LinearAlgebra/test/schur.jl +++ b/stdlib/LinearAlgebra/test/schur.jl @@ -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 diff --git a/stdlib/LinearAlgebra/test/special.jl b/stdlib/LinearAlgebra/test/special.jl index fbf63203f8ed8..54e1053521747 100644 --- a/stdlib/LinearAlgebra/test/special.jl +++ b/stdlib/LinearAlgebra/test/special.jl @@ -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;] diff --git a/stdlib/LinearAlgebra/test/svd.jl b/stdlib/LinearAlgebra/test/svd.jl index d3377d6412bf1..0182182e2902d 100644 --- a/stdlib/LinearAlgebra/test/svd.jl +++ b/stdlib/LinearAlgebra/test/svd.jl @@ -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 diff --git a/stdlib/LinearAlgebra/test/symmetric.jl b/stdlib/LinearAlgebra/test/symmetric.jl index 2d4329fab100c..51ba0e73b3aa1 100644 --- a/stdlib/LinearAlgebra/test/symmetric.jl +++ b/stdlib/LinearAlgebra/test/symmetric.jl @@ -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] ]) diff --git a/stdlib/LinearAlgebra/test/triangular.jl b/stdlib/LinearAlgebra/test/triangular.jl index 8a57ed9d97fb1..4156073d9a80c 100644 --- a/stdlib/LinearAlgebra/test/triangular.jl +++ b/stdlib/LinearAlgebra/test/triangular.jl @@ -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)) diff --git a/stdlib/LinearAlgebra/test/tridiag.jl b/stdlib/LinearAlgebra/test/tridiag.jl index 29e5942ea248f..91999af98b11b 100644 --- a/stdlib/LinearAlgebra/test/tridiag.jl +++ b/stdlib/LinearAlgebra/test/tridiag.jl @@ -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) diff --git a/stdlib/LinearAlgebra/test/uniformscaling.jl b/stdlib/LinearAlgebra/test/uniformscaling.jl index 02d2ea36f16df..9f871f55ee232 100644 --- a/stdlib/LinearAlgebra/test/uniformscaling.jl +++ b/stdlib/LinearAlgebra/test/uniformscaling.jl @@ -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 diff --git a/stdlib/Pkg/test/NastyGenerator.jl b/stdlib/Pkg/test/NastyGenerator.jl index 32d49422a33d4..df2d6705a384a 100644 --- a/stdlib/Pkg/test/NastyGenerator.jl +++ b/stdlib/Pkg/test/NastyGenerator.jl @@ -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] diff --git a/stdlib/SparseArrays/src/sparsematrix.jl b/stdlib/SparseArrays/src/sparsematrix.jl index 09298386dd2ee..1a1f129a1554f 100644 --- a/stdlib/SparseArrays/src/sparsematrix.jl +++ b/stdlib/SparseArrays/src/sparsematrix.jl @@ -1427,7 +1427,7 @@ distribution is used in case `rfn` is not specified. The optional `rng` argument specifies a random number generator, see [Random Numbers](@ref). # Examples -```jldoctest; setup = :(using Random; srand(1234)) +```jldoctest; setup = :(using Random; Random.seed(1234)) julia> sprand(Bool, 2, 2, 0.5) 2×2 SparseMatrixCSC{Bool,Int64} with 2 stored entries: [1, 1] = true @@ -1476,7 +1476,7 @@ where nonzero values are sampled from the normal distribution. The optional `rng argument specifies a random number generator, see [Random Numbers](@ref). # Examples -```jldoctest; setup = :(using Random; srand(0)) +```jldoctest; setup = :(using Random; Random.seed(0)) julia> sprandn(2, 2, 0.75) 2×2 SparseMatrixCSC{Float64,Int64} with 2 stored entries: [1, 1] = 0.586617 diff --git a/stdlib/SparseArrays/test/sparse.jl b/stdlib/SparseArrays/test/sparse.jl index c0f1dbc744cd6..ce9f153364621 100644 --- a/stdlib/SparseArrays/test/sparse.jl +++ b/stdlib/SparseArrays/test/sparse.jl @@ -1742,13 +1742,13 @@ end end @testset "sparse matrix opnormestinv" begin - srand(1234) + Random.seed(1234) Ac = sprandn(20,20,.5) + im* sprandn(20,20,.5) Aci = ceil.(Int64, 100*sprand(20,20,.5)) + im*ceil.(Int64, sprand(20,20,.5)) Ar = sprandn(20,20,.5) Ari = ceil.(Int64, 100*Ar) if Base.USE_GPL_LIBS - # NOTE: opnormestinv is probabilistic, so requires a fixed seed (set above in srand(1234)) + # NOTE: opnormestinv is probabilistic, so requires a fixed seed (set above in Random.seed(1234)) @test SparseArrays.opnormestinv(Ac,3) ≈ opnorm(inv(Array(Ac)),1) atol=1e-4 @test SparseArrays.opnormestinv(Aci,3) ≈ opnorm(inv(Array(Aci)),1) atol=1e-4 @test SparseArrays.opnormestinv(Ar) ≈ opnorm(inv(Array(Ar)),1) atol=1e-4 @@ -1795,7 +1795,7 @@ end end @testset "factorization" begin - srand(123) + Random.seed(123) local A A = sparse(Diagonal(rand(5))) + sprandn(5, 5, 0.2) + im*sprandn(5, 5, 0.2) A = A + copy(A') diff --git a/stdlib/SparseArrays/test/sparsevector.jl b/stdlib/SparseArrays/test/sparsevector.jl index c633cc444c0ba..2eee394fac830 100644 --- a/stdlib/SparseArrays/test/sparsevector.jl +++ b/stdlib/SparseArrays/test/sparsevector.jl @@ -1027,7 +1027,7 @@ end end @testset "dropzeros[!] with length=$m" for m in (10, 20, 30) - srand(123) + Random.seed(123) nzprob, targetnumposzeros, targetnumnegzeros = 0.4, 5, 5 v = sprand(m, nzprob) struczerosv = findall(x -> x == 0, v) diff --git a/stdlib/SuiteSparse/test/cholmod.jl b/stdlib/SuiteSparse/test/cholmod.jl index bf255e145b0ab..835e2c661166e 100644 --- a/stdlib/SuiteSparse/test/cholmod.jl +++ b/stdlib/SuiteSparse/test/cholmod.jl @@ -7,7 +7,7 @@ using Serialization using LinearAlgebra: issuccess, PosDefException # CHOLMOD tests -srand(123) +Random.seed(123) @testset "based on deps/SuiteSparse-4.0.2/CHOLMOD/Demo/" begin @@ -732,7 +732,7 @@ end end @testset "Check that Symmetric{SparseMatrixCSC} can be constructed from CHOLMOD.Sparse" begin - Int === Int32 && srand(124) + Int === Int32 && Random.seed(124) A = sprandn(10, 10, 0.1) B = CHOLMOD.Sparse(A) C = B'B diff --git a/stdlib/Test/src/Test.jl b/stdlib/Test/src/Test.jl index 9bba070655481..5eb256b340913 100644 --- a/stdlib/Test/src/Test.jl +++ b/stdlib/Test/src/Test.jl @@ -28,7 +28,7 @@ export TestSetException import Distributed: myid -using Random: srand, AbstractRNG, GLOBAL_RNG +using Random: AbstractRNG, GLOBAL_RNG using InteractiveUtils: gen_call_with_extracted_types #----------------------------------------------------------------------- @@ -1008,7 +1008,7 @@ method, which by default will return a list of the testset objects used in each iteration. Before the execution of the body of a `@testset`, there is an implicit -call to `srand(seed)` where `seed` is the current seed of the global RNG. +call to `Random.seed(seed)` where `seed` is the current seed of the global RNG. Moreover, after the execution of the body, the state of the global RNG is restored to what it was before the `@testset`. This is meant to ease reproducibility in case of failure, and to allow seamless @@ -1076,7 +1076,7 @@ function testset_beginend(args, tests, source) oldrng = copy(GLOBAL_RNG) try # GLOBAL_RNG is re-seeded with its own seed to ease reproduce a failed test - srand(GLOBAL_RNG.seed) + Random.seed(GLOBAL_RNG.seed) $(esc(tests)) catch err err isa InterruptException && rethrow(err) @@ -1142,7 +1142,7 @@ function testset_forloop(args, testloop, source) if !first_iteration pop_testset() push!(arr, finish(ts)) - # it's 1000 times faster to copy from tmprng rather than calling srand + # it's 1000 times faster to copy from tmprng rather than calling Random.seed copy!(GLOBAL_RNG, tmprng) end @@ -1163,7 +1163,7 @@ function testset_forloop(args, testloop, source) local first_iteration = true local ts local oldrng = copy(GLOBAL_RNG) - srand(GLOBAL_RNG.seed) + Random.seed(GLOBAL_RNG.seed) local tmprng = copy(GLOBAL_RNG) try $(Expr(:for, Expr(:block, [esc(v) for v in loopvars]...), blk)) @@ -1557,10 +1557,10 @@ function guardsrand(f::Function, r::AbstractRNG=GLOBAL_RNG) end end -"`guardsrand(f, seed)` is equivalent to running `srand(seed); f()` and +"`guardsrand(f, seed)` is equivalent to running `Random.seed(seed); f()` and then restoring the state of the global RNG as it was before." guardsrand(f::Function, seed::Union{Vector{UInt32},Integer}) = guardsrand() do - srand(seed) + Random.seed(seed) f() end diff --git a/stdlib/Test/test/runtests.jl b/stdlib/Test/test/runtests.jl index b509963a8957f..5c5f87a237eb7 100644 --- a/stdlib/Test/test/runtests.jl +++ b/stdlib/Test/test/runtests.jl @@ -612,18 +612,18 @@ let msg = split(read(pipeline(ignorestatus(`$(Base.julia_cmd()) --startup-file=n @test msg == rstrip(msg) end -@testset "test guarded srand" begin +@testset "test guarded Random.seed" begin seed = rand(UInt) orig = copy(Random.GLOBAL_RNG) @test guardsrand(()->rand(), seed) == guardsrand(()->rand(), seed) @test guardsrand(()->rand(Int), seed) == guardsrand(()->rand(Int), seed) r1, r2 = MersenneTwister(0), MersenneTwister(0) a, b = guardsrand(r1) do - srand(r1, 0) + Random.seed(r1, 0) rand(r1), rand(r1, Int) end::Tuple{Float64,Int} c, d = guardsrand(r2) do - srand(r2, 0) + Random.seed(r2, 0) rand(r2), rand(r2, Int) end::Tuple{Float64,Int} @test a == c == rand(r1) == rand(r2) @@ -741,7 +741,7 @@ end @testset "@testset preserves GLOBAL_RNG's state, and re-seeds it" begin # i.e. it behaves as if it was wrapped in a `guardsrand(GLOBAL_RNG.seed)` block seed = rand(UInt128) - srand(seed) + Random.seed(seed) a = rand() @testset begin # global RNG must re-seeded at the beginning of @testset @@ -752,7 +752,7 @@ end end # the @testset's above must have no consequence for rand() below b = rand() - srand(seed) + Random.seed(seed) @test a == rand() @test b == rand() end From 86a056352ac18955d564f555aeb5ee4a904d2a4c Mon Sep 17 00:00:00 2001 From: Olivier Date: Thu, 26 Jul 2018 17:24:02 +0100 Subject: [PATCH 04/16] Changed usage in test. --- test/sorting.jl | 2 +- test/testdefs.jl | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/sorting.jl b/test/sorting.jl index cfc6e86b9ae86..3759121b9ff54 100644 --- a/test/sorting.jl +++ b/test/sorting.jl @@ -227,7 +227,7 @@ function randn_with_nans(n,p) end @testset "advanced sorting" begin - srand(0xdeadbeef) + Random.seed(0xdeadbeef) for n in [0:10; 100; 101; 1000; 1001] local r r = -5:5 diff --git a/test/testdefs.jl b/test/testdefs.jl index 183162e62b355..1e59184b6485e 100644 --- a/test/testdefs.jl +++ b/test/testdefs.jl @@ -17,8 +17,8 @@ function runtests(name, path, isolate=true; seed=nothing) @eval(m, using Test, Random) ex = quote @timed @testset $"$name" begin - # srand(nothing) will fail - $seed != nothing && srand($seed) + # Random.seed(nothing) will fail + $seed != nothing && Random.seed($seed) include($"$path.jl") end end From b9a3b01d3d372255ac6d5a82c1617422d9a6e4c9 Mon Sep 17 00:00:00 2001 From: Olivier Date: Thu, 26 Jul 2018 17:25:44 +0100 Subject: [PATCH 05/16] Changed usage in doc. --- doc/src/devdocs/subarrays.md | 2 +- doc/src/manual/parallel-computing.md | 2 +- doc/src/manual/performance-tips.md | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/doc/src/devdocs/subarrays.md b/doc/src/devdocs/subarrays.md index 657d8a8909998..81a201d62d825 100644 --- a/doc/src/devdocs/subarrays.md +++ b/doc/src/devdocs/subarrays.md @@ -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); diff --git a/doc/src/manual/parallel-computing.md b/doc/src/manual/parallel-computing.md index a803696a1230b..39807b1f58c5a 100644 --- a/doc/src/manual/parallel-computing.md +++ b/doc/src/manual/parallel-computing.md @@ -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 ``` diff --git a/doc/src/manual/performance-tips.md b/doc/src/manual/performance-tips.md index c3fbc6f38d340..675f0bffe260c 100644 --- a/doc/src/manual/performance-tips.md +++ b/doc/src/manual/performance-tips.md @@ -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() @@ -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) @@ -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 @@ -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 From 28a76c474d1793af90ceb66a65e7ab86538f1999 Mon Sep 17 00:00:00 2001 From: Olivier Thill Date: Thu, 26 Jul 2018 20:08:19 +0100 Subject: [PATCH 06/16] Add explicit 'Random'.seed(). --- stdlib/Random/src/RNGs.jl | 4 +-- stdlib/Random/src/Random.jl | 14 ++++---- stdlib/Random/src/misc.jl | 2 +- stdlib/Random/test/runtests.jl | 58 +++++++++++++++++----------------- 4 files changed, 39 insertions(+), 39 deletions(-) diff --git a/stdlib/Random/src/RNGs.jl b/stdlib/Random/src/RNGs.jl index 4c8dcf15bc482..9652f3b1093c1 100644 --- a/stdlib/Random/src/RNGs.jl +++ b/stdlib/Random/src/RNGs.jl @@ -135,7 +135,7 @@ true ``` """ MersenneTwister(seed=nothing) = - seed(MersenneTwister(Vector{UInt32}(), DSFMT_state()), seed) + Random.seed(MersenneTwister(Vector{UInt32}(), DSFMT_state()), seed) function copy!(dst::MersenneTwister, src::MersenneTwister) @@ -287,7 +287,7 @@ end 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) +seed(seed::Union{Integer,Vector{UInt32}}) = Random.seed(GLOBAL_RNG, seed) ### Global RNG (must be defined after seed) diff --git a/stdlib/Random/src/Random.jl b/stdlib/Random/src/Random.jl index 191858bd54ab4..3e4f9f0171fdf 100644 --- a/stdlib/Random/src/Random.jl +++ b/stdlib/Random/src/Random.jl @@ -17,7 +17,7 @@ using Serialization import Serialization: serialize, deserialize import Base: rand, randn -export rand!, randn!, +export zrand!, randn!, randexp, randexp!, bitrand, randstring, @@ -264,7 +264,7 @@ rand( ::Type{X}, d::Integer, dims::Integer...) where {X} = rand(X function __init__() try - srand() + seed() catch ex Base.showerror_nostdio(ex, "WARNING: Error during initialization of module Random") @@ -352,14 +352,14 @@ object initialized with the same seed. # Examples ```julia-repl -julia> seed(1234); +julia> Random.seed(1234); julia> x1 = rand(2) 2-element Array{Float64,1}: 0.590845 0.766797 -julia> seed(1234); +julia> Random.seed(1234); julia> x2 = rand(2) 2-element Array{Float64,1}: @@ -372,13 +372,13 @@ true julia> rng = MersenneTwister(1234); rand(rng, 2) == x1 true -julia> MersenneTwister(1) == seed(rng, 1) +julia> MersenneTwister(1) == Random.seed(rng, 1) true -julia> rand(seed(rng), Bool) # not reproducible +julia> rand(Random.seed(rng), Bool) # not reproducible true -julia> rand(seed(rng), Bool) +julia> rand(Random.seed(rng), Bool) false julia> rand(MersenneTwister(), Bool) # not reproducible either diff --git a/stdlib/Random/src/misc.jl b/stdlib/Random/src/misc.jl index 77b3a7a4d949f..0676999903724 100644 --- a/stdlib/Random/src/misc.jl +++ b/stdlib/Random/src/misc.jl @@ -52,7 +52,7 @@ number generator, see [Random Numbers](@ref). # Examples ```jldoctest -julia> seed(0); randstring() +julia> Random.seed(0); randstring() "0IPrGg0J" julia> randstring(MersenneTwister(0), 'a':'z', 6) diff --git a/stdlib/Random/test/runtests.jl b/stdlib/Random/test/runtests.jl index eea996bd60717..be51921b02fbe 100644 --- a/stdlib/Random/test/runtests.jl +++ b/stdlib/Random/test/runtests.jl @@ -15,7 +15,7 @@ using Random: Sampler, SamplerRangeFast, SamplerRangeInt, MT_CACHE_F, MT_CACHE_I import Future # randjump @testset "Issue #6573" begin - seed(0) + Random.seed(0) rand() x = rand(384) @test findall(x .== rand()) == [] @@ -131,9 +131,9 @@ for T in [UInt32, UInt64, UInt128, Int128] @test size(r) == (1, 2) @test typeof(r) == Matrix{BigInt} guardsrand() do - seed(0) + Random.seed(0) r = rand(s) - seed(0) + Random.seed(0) @test rand(s) == r end end @@ -225,13 +225,13 @@ randmtzig_fill_ziggurat_tables() #same random numbers on for small ranges on all systems guardsrand() do seed = rand(UInt) - seed(seed) + Random.seed(seed) r = map(Int64, rand(map(Int32, 97:122))) - seed(seed) + Random.seed(seed) @test r == rand(map(Int64, 97:122)) - seed(seed) + Random.seed(seed) r = map(UInt64, rand(map(UInt32, 97:122))) - seed(seed) + Random.seed(seed) @test r == rand(map(UInt64, 97:122)) end @@ -266,7 +266,7 @@ let mt = MersenneTwister(0) 0x4b54632b4619f4eca22675166784d229][i] end - seed(mt, 0) + Random.seed(mt, 0) for (i,T) in enumerate([Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, Float16, Float32]) A = Vector{T}(undef, 16) B = Vector{T}(undef, 31) @@ -278,7 +278,7 @@ let mt = MersenneTwister(0) 61881313582466480231846019869039259750, Float16(0.38672), 0.20027375f0][i] end - seed(mt, 0) + Random.seed(mt, 0) AF64 = Vector{Float64}(undef, Random.dsfmt_get_min_array_size()-1) @test rand!(mt, AF64)[end] == 0.957735065345398 @test rand!(mt, AF64)[end] == 0.6492481059865669 @@ -304,7 +304,7 @@ let mt = MersenneTwister(0) for A in (a, b, c) local A - seed(mt, 0) + Random.seed(mt, 0) rand(mt) # this is to fill mt.vals, cf. #9040 rand!(mt, A) # must not segfault even if Int(pointer(A)) % 16 != 0 @test A[end-4:end] == [0.3371041633752143, 0.41147647589610803, 0.6063082992397912, 0.9103565379264364, 0.16456579813368521] @@ -538,8 +538,8 @@ end # test that the following is not an error (#16925) guardsrand() do - seed(typemax(UInt)) - seed(typemax(UInt128)) + Random.seed(typemax(UInt)) + Random.seed(typemax(UInt128)) end # copy, == and hash @@ -584,24 +584,24 @@ let seed = rand(UInt32, 10) # RNGs do not share their seed in randjump let r2 = Future.randjump(r, big(10)^20) @test r.seed !== r2.seed - seed(r2) + Random.seed(r2) @test seed == r.seed != r2.seed end resize!(seed, 4) @test r.seed != seed end -# seed(rng, ...) returns rng (#21248) +# Random.seed(rng, ...) returns rng (#21248) guardsrand() do g = Random.GLOBAL_RNG m = MersenneTwister(0) - @test seed() === g - @test seed(rand(UInt)) === g - @test seed(rand(UInt32, rand(1:10))) === g - @test seed(m) === m - @test seed(m, rand(UInt)) === m - @test seed(m, rand(UInt32, rand(1:10))) === m - @test seed(m, rand(1:10)) === m + @test Random.seed() === g + @test Random.seed(rand(UInt)) === g + @test Random.seed(rand(UInt32, rand(1:10))) === g + @test Random.seed(m) === m + @test Random.seed(m, rand(UInt)) === m + @test Random.seed(m, rand(UInt32, rand(1:10))) === m + @test Random.seed(m, rand(1:10)) === m end # Issue 20062 - ensure internal functions reserve_1, reserve are type-stable @@ -633,7 +633,7 @@ end # this shouldn't crash (#22403) @test_throws ArgumentError rand!(Union{UInt,Int}[1, 2, 3]) -@testset "$RNG() & seed(rng::$RNG) initializes randomly" for RNG in (MersenneTwister, RandomDevice) +@testset "$RNG() & Random.seed(rng::$RNG) initializes randomly" for RNG in (MersenneTwister, RandomDevice) m = RNG() a = rand(m, Int) m = RNG() @@ -642,22 +642,22 @@ end m = RNG(nothing) b = rand(m, Int) @test b != a - seed(m) + Random.seed(m) c = rand(m, Int) @test c ∉ (a, b) - seed(m) + Random.seed(m) @test rand(m, Int) ∉ (a, b, c) - seed(m, nothing) + Random.seed(m, nothing) d = rand(m, Int) @test d ∉ (a, b, c) - seed(m, nothing) + Random.seed(m, nothing) @test rand(m, Int) ∉ (a, b, c, d) end -@testset "MersenneTwister($seed) & seed(m::MersenneTwister, $seed) produce the same stream" for seed in [0:5; 10000:10005] +@testset "MersenneTwister($seed) & Random.seed(m::MersenneTwister, $seed) produce the same stream" for seed in [0:5; 10000:10005] m = MersenneTwister(seed) a = [rand(m) for _=1:100] - seed(m, seed) + Random.seed(m, seed) @test a == [rand(m) for _=1:100] end @@ -669,7 +669,7 @@ end @testset "rand(::$(typeof(RNG)), ::UnitRange{$T}" for RNG ∈ (MersenneTwister(), RandomDevice()), T ∈ (Int32, UInt32, Int64, Int128, UInt128) - RNG isa MersenneTwister && seed(RNG, rand(UInt128)) # for reproducibility + RNG isa MersenneTwister && Random.seed(RNG, rand(UInt128)) # for reproducibility r = T(1):T(108) @test rand(RNG, SamplerRangeFast(r)) ∈ r end From 91f5f3c786c2e8f97718ca6e95dc966c79908be6 Mon Sep 17 00:00:00 2001 From: Olivier Thill Date: Fri, 27 Jul 2018 09:07:18 +0100 Subject: [PATCH 07/16] Amend Deprecation. --- stdlib/Random/src/deprecated.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/Random/src/deprecated.jl b/stdlib/Random/src/deprecated.jl index cc227967f8dc3..ec7acbff7e38f 100644 --- a/stdlib/Random/src/deprecated.jl +++ b/stdlib/Random/src/deprecated.jl @@ -38,5 +38,5 @@ end # PR #25668 @deprecate RandomDevice(unlimited::Bool) RandomDevice(; unlimited=unlimited) -# PR #Current -@deprecate srand Random.seed +# PR #28295 +@deprecate srand Random.seed false From 049bc314109e53f7191f6b873827876c1b1344d4 Mon Sep 17 00:00:00 2001 From: Olivier Thill Date: Fri, 27 Jul 2018 09:21:24 +0100 Subject: [PATCH 08/16] Ensure proper 'using Random's. --- stdlib/SuiteSparse/test/cholmod.jl | 1 + stdlib/Test/src/Test.jl | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/stdlib/SuiteSparse/test/cholmod.jl b/stdlib/SuiteSparse/test/cholmod.jl index 835e2c661166e..a439226c4cfff 100644 --- a/stdlib/SuiteSparse/test/cholmod.jl +++ b/stdlib/SuiteSparse/test/cholmod.jl @@ -3,6 +3,7 @@ using SuiteSparse.CHOLMOD using DelimitedFiles using Test +using Random using Serialization using LinearAlgebra: issuccess, PosDefException diff --git a/stdlib/Test/src/Test.jl b/stdlib/Test/src/Test.jl index 5eb256b340913..e8503bb2e0fee 100644 --- a/stdlib/Test/src/Test.jl +++ b/stdlib/Test/src/Test.jl @@ -28,7 +28,7 @@ export TestSetException import Distributed: myid -using Random: AbstractRNG, GLOBAL_RNG +using Random: Random, AbstractRNG, GLOBAL_RNG using InteractiveUtils: gen_call_with_extracted_types #----------------------------------------------------------------------- From 31cf36b8fd4b199ca4952673451589bca2de8d60 Mon Sep 17 00:00:00 2001 From: Olivier Thill Date: Fri, 27 Jul 2018 10:01:35 +0100 Subject: [PATCH 09/16] Add News. --- NEWS.md | 5 ++++- stdlib/Random/docs/src/index.md | 8 +------- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/NEWS.md b/NEWS.md index 0a5a83ec84fb5..dc5a87496d3fe 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1323,6 +1323,8 @@ Deprecated or removed * `squeeze` is deprecated in favor of `dropdims`. + * `srand` is deprecated in favor of the unexported `Random.seed` ([#28295]). + Command-line option changes --------------------------- @@ -1336,7 +1338,7 @@ Command-line option changes * The option `--precompiled` has been renamed to `--sysimage-native-code` ([#23054]). - * The option `--compilecache` has been renamed to `--compiled-modules` ([#23054]). + * The option `--compilecache` has been renamed to `--compiled-modules` ([#27726]). [#330]: https://github.com/JuliaLang/julia/issues/330 @@ -1672,6 +1674,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 diff --git a/stdlib/Random/docs/src/index.md b/stdlib/Random/docs/src/index.md index 9b7f7e47ffe67..8e008e1cbd0db 100644 --- a/stdlib/Random/docs/src/index.md +++ b/stdlib/Random/docs/src/index.md @@ -30,12 +30,6 @@ Additionally, normal and exponential distributions are implemented for some `Abs ## Random generation functions ```@docs -<<<<<<< a707aacc5c3bdc059bc8c1ced6e3324d5bcd38b1 -======= -Random.seed -Random.MersenneTwister -Random.RandomDevice ->>>>>>> Switched usage in stdlb/Random and added deprecation. Random.rand Random.rand! Random.bitrand @@ -62,7 +56,7 @@ Random.shuffle! ## Generators (creation and seeding) ```@docs -Random.srand +Random.seed! Random.MersenneTwister Random.RandomDevice ``` From 5b645413b44af2c4dfc7ce09662f4a6695cc4e23 Mon Sep 17 00:00:00 2001 From: Olivier Thill Date: Fri, 27 Jul 2018 10:59:29 +0100 Subject: [PATCH 10/16] Fix typo from earlier commit. --- stdlib/Random/src/Random.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/Random/src/Random.jl b/stdlib/Random/src/Random.jl index 3e4f9f0171fdf..2b28cf2359082 100644 --- a/stdlib/Random/src/Random.jl +++ b/stdlib/Random/src/Random.jl @@ -17,7 +17,7 @@ using Serialization import Serialization: serialize, deserialize import Base: rand, randn -export zrand!, randn!, +export rand!, randn!, randexp, randexp!, bitrand, randstring, From f242e575a258f73366baa8a2c27b1973db1e6e5c Mon Sep 17 00:00:00 2001 From: Olivier Thill Date: Fri, 27 Jul 2018 11:56:55 +0100 Subject: [PATCH 11/16] Change internal Test.guardsrand to Test.guardseed to match Random.seed. --- stdlib/Random/test/runtests.jl | 10 +++++----- stdlib/SparseArrays/test/sparse.jl | 6 +++--- stdlib/Statistics/test/runtests.jl | 4 ++-- stdlib/Test/src/Test.jl | 10 +++++----- stdlib/Test/test/runtests.jl | 12 ++++++------ test/error.jl | 2 +- 6 files changed, 22 insertions(+), 22 deletions(-) diff --git a/stdlib/Random/test/runtests.jl b/stdlib/Random/test/runtests.jl index be51921b02fbe..b219d28aa8de5 100644 --- a/stdlib/Random/test/runtests.jl +++ b/stdlib/Random/test/runtests.jl @@ -1,7 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license using Test, SparseArrays -using Test: guardsrand +using Test: guardseed const BASE_TEST_PATH = joinpath(Sys.BINDIR, "..", "share", "julia", "test") isdefined(Main, :OffsetArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "OffsetArrays.jl")) @@ -130,7 +130,7 @@ for T in [UInt32, UInt64, UInt128, Int128] r = rand(s, 1, 2) @test size(r) == (1, 2) @test typeof(r) == Matrix{BigInt} - guardsrand() do + guardseed() do Random.seed(0) r = rand(s) Random.seed(0) @@ -223,7 +223,7 @@ randmtzig_fill_ziggurat_tables() @test all(fe == Random.fe) #same random numbers on for small ranges on all systems -guardsrand() do +guardseed() do seed = rand(UInt) Random.seed(seed) r = map(Int64, rand(map(Int32, 97:122))) @@ -537,7 +537,7 @@ end end # test that the following is not an error (#16925) -guardsrand() do +guardseed() do Random.seed(typemax(UInt)) Random.seed(typemax(UInt128)) end @@ -592,7 +592,7 @@ let seed = rand(UInt32, 10) end # Random.seed(rng, ...) returns rng (#21248) -guardsrand() do +guardseed() do g = Random.GLOBAL_RNG m = MersenneTwister(0) @test Random.seed() === g diff --git a/stdlib/SparseArrays/test/sparse.jl b/stdlib/SparseArrays/test/sparse.jl index ce9f153364621..35386bcea8676 100644 --- a/stdlib/SparseArrays/test/sparse.jl +++ b/stdlib/SparseArrays/test/sparse.jl @@ -7,7 +7,7 @@ using SparseArrays using LinearAlgebra using Base.Printf: @printf using Random -using Test: guardsrand +using Test: guardseed @testset "issparse" begin @test issparse(sparse(fill(1,5,5))) @@ -1442,7 +1442,7 @@ end end @testset "droptol" begin - local A = guardsrand(1234321) do + local A = guardseed(1234321) do triu(sprand(10, 10, 0.2)) end @test SparseArrays.droptol!(A, 0.01).colptr == [1,1,1,2,2,3,4,6,6,7,9] @@ -2059,7 +2059,7 @@ end @testset "reverse search direction if step < 0 #21986" begin local A, B - A = guardsrand(1234) do + A = guardseed(1234) do sprand(5, 5, 1/5) end A = max.(A, copy(A')) diff --git a/stdlib/Statistics/test/runtests.jl b/stdlib/Statistics/test/runtests.jl index 7733e9ae6166a..15dfea2e153bb 100644 --- a/stdlib/Statistics/test/runtests.jl +++ b/stdlib/Statistics/test/runtests.jl @@ -1,7 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license using Statistics, Test, Random, LinearAlgebra, SparseArrays -using Test: guardsrand +using Test: guardseed @testset "middle" begin @test middle(3) === 3.0 @@ -599,7 +599,7 @@ end n = 10 p = 5 np2 = div(n*p, 2) - nzvals, x_sparse = guardsrand(1) do + nzvals, x_sparse = guardseed(1) do if elty <: Real nzvals = randn(np2) else diff --git a/stdlib/Test/src/Test.jl b/stdlib/Test/src/Test.jl index e8503bb2e0fee..84ad64cd498fd 100644 --- a/stdlib/Test/src/Test.jl +++ b/stdlib/Test/src/Test.jl @@ -1070,7 +1070,7 @@ function testset_beginend(args, tests, source) # which is needed for backtrace scrubbing to work correctly. while false; end push_testset(ts) - # we reproduce the logic of guardsrand, but this function + # we reproduce the logic of guardseed, but this function # cannot be used as it changes slightly the semantic of @testset, # by wrapping the body in a function oldrng = copy(GLOBAL_RNG) @@ -1546,9 +1546,9 @@ Base.setindex!(a::GenericArray, x, i...) = a.a[i...] = x Base.similar(A::GenericArray, s::Integer...) = GenericArray(similar(A.a, s...)) -"`guardsrand(f)` runs the function `f()` and then restores the +"`guardseed(f)` runs the function `f()` and then restores the state of the global RNG as it was before." -function guardsrand(f::Function, r::AbstractRNG=GLOBAL_RNG) +function guardseed(f::Function, r::AbstractRNG=GLOBAL_RNG) old = copy(r) try f() @@ -1557,9 +1557,9 @@ function guardsrand(f::Function, r::AbstractRNG=GLOBAL_RNG) end end -"`guardsrand(f, seed)` is equivalent to running `Random.seed(seed); f()` and +"`guardseed(f, seed)` is equivalent to running `Random.seed(seed); f()` and then restoring the state of the global RNG as it was before." -guardsrand(f::Function, seed::Union{Vector{UInt32},Integer}) = guardsrand() do +guardseed(f::Function, seed::Union{Vector{UInt32},Integer}) = guardseed() do Random.seed(seed) f() end diff --git a/stdlib/Test/test/runtests.jl b/stdlib/Test/test/runtests.jl index 5c5f87a237eb7..5becb748eaf5e 100644 --- a/stdlib/Test/test/runtests.jl +++ b/stdlib/Test/test/runtests.jl @@ -1,7 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license using Test, Distributed, Random -using Test: guardsrand +using Test: guardseed import Logging: Debug, Info, Warn @@ -615,14 +615,14 @@ end @testset "test guarded Random.seed" begin seed = rand(UInt) orig = copy(Random.GLOBAL_RNG) - @test guardsrand(()->rand(), seed) == guardsrand(()->rand(), seed) - @test guardsrand(()->rand(Int), seed) == guardsrand(()->rand(Int), seed) + @test guardseed(()->rand(), seed) == guardseed(()->rand(), seed) + @test guardseed(()->rand(Int), seed) == guardseed(()->rand(Int), seed) r1, r2 = MersenneTwister(0), MersenneTwister(0) - a, b = guardsrand(r1) do + a, b = guardseed(r1) do Random.seed(r1, 0) rand(r1), rand(r1, Int) end::Tuple{Float64,Int} - c, d = guardsrand(r2) do + c, d = guardseed(r2) do Random.seed(r2, 0) rand(r2), rand(r2, Int) end::Tuple{Float64,Int} @@ -739,7 +739,7 @@ end end @testset "@testset preserves GLOBAL_RNG's state, and re-seeds it" begin - # i.e. it behaves as if it was wrapped in a `guardsrand(GLOBAL_RNG.seed)` block + # i.e. it behaves as if it was wrapped in a `guardseed(GLOBAL_RNG.seed)` block seed = rand(UInt128) Random.seed(seed) a = rand() diff --git a/test/error.jl b/test/error.jl index 8a9846cc8d2bf..741ae762acbe3 100644 --- a/test/error.jl +++ b/test/error.jl @@ -6,7 +6,7 @@ @test maximum(ExponentialBackOff(n=10, max_delay=0.06)) == 0.06 ratio(x) = x[2:end]./x[1:end-1] @test all(x->x ≈ 10.0, ratio(collect(ExponentialBackOff(n=10, max_delay=Inf, factor=10, jitter=0.0)))) - Test.guardsrand(12345) do + Test.guardseed(12345) do x = ratio(collect(ExponentialBackOff(n=100, max_delay=Inf, factor=1, jitter=0.1))) xm = sum(x) / length(x) @test (xm - 1.0) < 1e-4 From 409862233c7fce387c446915b6a95f9766565053 Mon Sep 17 00:00:00 2001 From: Olivier Thill Date: Fri, 27 Jul 2018 12:17:54 +0100 Subject: [PATCH 12/16] Change a using statment where I tried to be to smart. --- stdlib/Test/src/Test.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stdlib/Test/src/Test.jl b/stdlib/Test/src/Test.jl index 84ad64cd498fd..20f671a1b50bf 100644 --- a/stdlib/Test/src/Test.jl +++ b/stdlib/Test/src/Test.jl @@ -28,7 +28,8 @@ export TestSetException import Distributed: myid -using Random: Random, AbstractRNG, GLOBAL_RNG +using Random +using Random: AbstractRNG, GLOBAL_RNG using InteractiveUtils: gen_call_with_extracted_types #----------------------------------------------------------------------- From 4fd00b59305702fa10f4327057343b2303d1e516 Mon Sep 17 00:00:00 2001 From: Olivier Thill Date: Fri, 27 Jul 2018 12:49:52 +0100 Subject: [PATCH 13/16] Fix error in my News commit. --- NEWS.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index dc5a87496d3fe..836f0542ff527 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1323,7 +1323,7 @@ Deprecated or removed * `squeeze` is deprecated in favor of `dropdims`. - * `srand` is deprecated in favor of the unexported `Random.seed` ([#28295]). + * `srand` is deprecated in favor of the unexported `Random.seed` ([#27726]). Command-line option changes --------------------------- @@ -1338,7 +1338,7 @@ Command-line option changes * The option `--precompiled` has been renamed to `--sysimage-native-code` ([#23054]). - * The option `--compilecache` has been renamed to `--compiled-modules` ([#27726]). + * The option `--compilecache` has been renamed to `--compiled-modules` ([#23054]). [#330]: https://github.com/JuliaLang/julia/issues/330 From 75ff13e3183a02e622d7bb72856b5304736fd394 Mon Sep 17 00:00:00 2001 From: Olivier Thill Date: Fri, 27 Jul 2018 20:46:31 +0100 Subject: [PATCH 14/16] Change deprecation again. --- stdlib/Random/src/deprecated.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/Random/src/deprecated.jl b/stdlib/Random/src/deprecated.jl index ec7acbff7e38f..f668908b0405b 100644 --- a/stdlib/Random/src/deprecated.jl +++ b/stdlib/Random/src/deprecated.jl @@ -39,4 +39,4 @@ end @deprecate RandomDevice(unlimited::Bool) RandomDevice(; unlimited=unlimited) # PR #28295 -@deprecate srand Random.seed false +@deprecate srand Random.seed From 7b42b443bf8bb2cb7b914f810a1aa3f63f197326 Mon Sep 17 00:00:00 2001 From: Olivier Thill Date: Fri, 27 Jul 2018 21:08:30 +0100 Subject: [PATCH 15/16] Renamed Random.seed to Random.seed! --- NEWS.md | 2 +- doc/src/devdocs/subarrays.md | 2 +- doc/src/manual/parallel-computing.md | 2 +- doc/src/manual/performance-tips.md | 8 +-- stdlib/LinearAlgebra/test/bidiag.jl | 2 +- stdlib/LinearAlgebra/test/blas.jl | 2 +- stdlib/LinearAlgebra/test/bunchkaufman.jl | 2 +- stdlib/LinearAlgebra/test/cholesky.jl | 4 +- stdlib/LinearAlgebra/test/dense.jl | 2 +- stdlib/LinearAlgebra/test/diagonal.jl | 2 +- stdlib/LinearAlgebra/test/eigen.jl | 2 +- stdlib/LinearAlgebra/test/generic.jl | 2 +- stdlib/LinearAlgebra/test/hessenberg.jl | 2 +- stdlib/LinearAlgebra/test/lapack.jl | 8 +-- stdlib/LinearAlgebra/test/lq.jl | 2 +- stdlib/LinearAlgebra/test/lu.jl | 4 +- stdlib/LinearAlgebra/test/pinv.jl | 2 +- stdlib/LinearAlgebra/test/qr.jl | 2 +- stdlib/LinearAlgebra/test/schur.jl | 2 +- stdlib/LinearAlgebra/test/special.jl | 2 +- stdlib/LinearAlgebra/test/svd.jl | 2 +- stdlib/LinearAlgebra/test/symmetric.jl | 2 +- stdlib/LinearAlgebra/test/triangular.jl | 2 +- stdlib/LinearAlgebra/test/tridiag.jl | 4 +- stdlib/LinearAlgebra/test/uniformscaling.jl | 2 +- stdlib/Pkg/test/NastyGenerator.jl | 2 +- stdlib/Random/docs/src/index.md | 6 +++ stdlib/Random/src/RNGs.jl | 18 +++---- stdlib/Random/src/Random.jl | 22 ++++---- stdlib/Random/src/deprecated.jl | 2 +- stdlib/Random/src/misc.jl | 2 +- stdlib/Random/test/runtests.jl | 58 ++++++++++----------- stdlib/SparseArrays/src/sparsematrix.jl | 4 +- stdlib/SparseArrays/test/sparse.jl | 6 +-- stdlib/SparseArrays/test/sparsevector.jl | 2 +- stdlib/SuiteSparse/test/cholmod.jl | 4 +- stdlib/Test/src/Test.jl | 12 ++--- stdlib/Test/test/runtests.jl | 10 ++-- test/sorting.jl | 2 +- test/testdefs.jl | 4 +- 40 files changed, 114 insertions(+), 108 deletions(-) diff --git a/NEWS.md b/NEWS.md index 836f0542ff527..922e927d65b70 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1323,7 +1323,7 @@ Deprecated or removed * `squeeze` is deprecated in favor of `dropdims`. - * `srand` is deprecated in favor of the unexported `Random.seed` ([#27726]). + * `srand` is deprecated in favor of the unexported `Random.seed!` ([#27726]). Command-line option changes --------------------------- diff --git a/doc/src/devdocs/subarrays.md b/doc/src/devdocs/subarrays.md index 81a201d62d825..6b91083b1d094 100644 --- a/doc/src/devdocs/subarrays.md +++ b/doc/src/devdocs/subarrays.md @@ -37,7 +37,7 @@ cartesian, rather than linear, indexing. Consider making 2d slices of a 3d array: ```@meta -DocTestSetup = :(import Random; Random.seed(1234)) +DocTestSetup = :(import Random; Random.seed!(1234)) ``` ```jldoctest subarray julia> A = rand(2,3,4); diff --git a/doc/src/manual/parallel-computing.md b/doc/src/manual/parallel-computing.md index 39807b1f58c5a..5f18d97e75aa4 100644 --- a/doc/src/manual/parallel-computing.md +++ b/doc/src/manual/parallel-computing.md @@ -418,7 +418,7 @@ julia> function g() end g (generic function with 1 method) -julia> Random.seed(1); g() # the result for a single thread is 1000 +julia> Random.seed!(1); g() # the result for a single thread is 1000 781 ``` diff --git a/doc/src/manual/performance-tips.md b/doc/src/manual/performance-tips.md index 675f0bffe260c..cd81e9eaf0c59 100644 --- a/doc/src/manual/performance-tips.md +++ b/doc/src/manual/performance-tips.md @@ -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; Random.seed(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() @@ -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; Random.seed(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) @@ -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; Random.seed(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 @@ -601,7 +601,7 @@ julia> strange_twos(3) This should be written as: -```jldoctest; setup = :(using Random; Random.seed(1234)) +```jldoctest; setup = :(using Random; Random.seed!(1234)) julia> function fill_twos!(a) for i = eachindex(a) a[i] = 2 diff --git a/stdlib/LinearAlgebra/test/bidiag.jl b/stdlib/LinearAlgebra/test/bidiag.jl index bf4cad2aa4dd3..3dd2d1ae9913f 100644 --- a/stdlib/LinearAlgebra/test/bidiag.jl +++ b/stdlib/LinearAlgebra/test/bidiag.jl @@ -8,7 +8,7 @@ using LinearAlgebra: BlasReal, BlasFloat include("testutils.jl") # test_approx_eq_modphase n = 10 #Size of test matrix -Random.seed(1) +Random.seed!(1) @testset for relty in (Int, Float32, Float64, BigFloat), elty in (relty, Complex{relty}) if relty <: AbstractFloat diff --git a/stdlib/LinearAlgebra/test/blas.jl b/stdlib/LinearAlgebra/test/blas.jl index e3c952324631c..239cd6842b4a9 100644 --- a/stdlib/LinearAlgebra/test/blas.jl +++ b/stdlib/LinearAlgebra/test/blas.jl @@ -5,7 +5,7 @@ module TestBLAS using Test, LinearAlgebra, Random using LinearAlgebra: BlasReal, BlasComplex -Random.seed(100) +Random.seed!(100) ## BLAS tests - testing the interface code to BLAS routines @testset for elty in [Float32, Float64, ComplexF32, ComplexF64] @testset "syr2k!" begin diff --git a/stdlib/LinearAlgebra/test/bunchkaufman.jl b/stdlib/LinearAlgebra/test/bunchkaufman.jl index b985d138e7ac7..0333685579bc9 100644 --- a/stdlib/LinearAlgebra/test/bunchkaufman.jl +++ b/stdlib/LinearAlgebra/test/bunchkaufman.jl @@ -12,7 +12,7 @@ n = 10 n1 = div(n, 2) n2 = 2*n1 -Random.seed(1234321) +Random.seed!(1234321) areal = randn(n,n)/2 aimg = randn(n,n)/2 diff --git a/stdlib/LinearAlgebra/test/cholesky.jl b/stdlib/LinearAlgebra/test/cholesky.jl index 488871a108284..f8e1276eb56a4 100644 --- a/stdlib/LinearAlgebra/test/cholesky.jl +++ b/stdlib/LinearAlgebra/test/cholesky.jl @@ -39,7 +39,7 @@ end n1 = div(n, 2) n2 = 2*n1 - Random.seed(1234321) + Random.seed!(1234321) areal = randn(n,n)/2 aimg = randn(n,n)/2 @@ -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 Random.seed(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])) diff --git a/stdlib/LinearAlgebra/test/dense.jl b/stdlib/LinearAlgebra/test/dense.jl index b9803b04b1725..5ec530668d7e2 100644 --- a/stdlib/LinearAlgebra/test/dense.jl +++ b/stdlib/LinearAlgebra/test/dense.jl @@ -15,7 +15,7 @@ n = 10 n1 = div(n, 2) n2 = 2*n1 -Random.seed(1234321) +Random.seed!(1234321) @testset "Matrix condition number" begin ainit = rand(n,n) diff --git a/stdlib/LinearAlgebra/test/diagonal.jl b/stdlib/LinearAlgebra/test/diagonal.jl index 5d6385150ff43..ee11b5db9854b 100644 --- a/stdlib/LinearAlgebra/test/diagonal.jl +++ b/stdlib/LinearAlgebra/test/diagonal.jl @@ -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 -Random.seed(1) +Random.seed!(1) @testset for relty in (Float32, Float64, BigFloat), elty in (relty, Complex{relty}) dd=convert(Vector{elty}, randn(n)) diff --git a/stdlib/LinearAlgebra/test/eigen.jl b/stdlib/LinearAlgebra/test/eigen.jl index d712802781850..a683202feb2d2 100644 --- a/stdlib/LinearAlgebra/test/eigen.jl +++ b/stdlib/LinearAlgebra/test/eigen.jl @@ -11,7 +11,7 @@ n = 10 n1 = div(n, 2) n2 = 2*n1 -Random.seed(1234321) +Random.seed!(1234321) areal = randn(n,n)/2 aimg = randn(n,n)/2 diff --git a/stdlib/LinearAlgebra/test/generic.jl b/stdlib/LinearAlgebra/test/generic.jl index 1a817ef1602a3..1c38883356efb 100644 --- a/stdlib/LinearAlgebra/test/generic.jl +++ b/stdlib/LinearAlgebra/test/generic.jl @@ -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)) -Random.seed(123) +Random.seed!(123) n = 5 # should be odd diff --git a/stdlib/LinearAlgebra/test/hessenberg.jl b/stdlib/LinearAlgebra/test/hessenberg.jl index 9616ed637b25a..4442c11282fef 100644 --- a/stdlib/LinearAlgebra/test/hessenberg.jl +++ b/stdlib/LinearAlgebra/test/hessenberg.jl @@ -5,7 +5,7 @@ module TestHessenberg using Test, LinearAlgebra, Random let n = 10 - Random.seed(1234321) + Random.seed!(1234321) Areal = randn(n,n)/2 Aimg = randn(n,n)/2 diff --git a/stdlib/LinearAlgebra/test/lapack.jl b/stdlib/LinearAlgebra/test/lapack.jl index 6539ffcc4ecce..610d7f0e5a96d 100644 --- a/stdlib/LinearAlgebra/test/lapack.jl +++ b/stdlib/LinearAlgebra/test/lapack.jl @@ -11,7 +11,7 @@ using LinearAlgebra: BlasInt @test_throws ArgumentError LinearAlgebra.LAPACK.chktrans('Z') @testset "syevr" begin - Random.seed(123) + Random.seed!(123) Ainit = randn(5,5) @testset for elty in (Float32, Float64, ComplexF32, ComplexF64) if elty == ComplexF32 || elty == ComplexF64 @@ -208,7 +208,7 @@ end @testset "gels" begin @testset for elty in (Float32, Float64, ComplexF32, ComplexF64) - Random.seed(913) + Random.seed!(913) A = rand(elty,10,10) X = rand(elty,10) B,Y,z = LAPACK.gels!('N',copy(A),copy(X)) @@ -443,7 +443,7 @@ end @testset "sysv" begin @testset for elty in (Float32, Float64, ComplexF32, ComplexF64) - Random.seed(123) + Random.seed!(123) A = rand(elty,10,10) A = A + transpose(A) #symmetric! b = rand(elty,10) @@ -456,7 +456,7 @@ end @testset "hesv" begin @testset for elty in (ComplexF32, ComplexF64) - Random.seed(935) + Random.seed!(935) A = rand(elty,10,10) A = A + A' #hermitian! b = rand(elty,10) diff --git a/stdlib/LinearAlgebra/test/lq.jl b/stdlib/LinearAlgebra/test/lq.jl index dcc4d84eb6716..f690896ebdc40 100644 --- a/stdlib/LinearAlgebra/test/lq.jl +++ b/stdlib/LinearAlgebra/test/lq.jl @@ -11,7 +11,7 @@ n = 10 n1 = div(n, 2) n2 = 2*n1 -Random.seed(1234321) +Random.seed!(1234321) areal = randn(n,n)/2 aimg = randn(n,n)/2 diff --git a/stdlib/LinearAlgebra/test/lu.jl b/stdlib/LinearAlgebra/test/lu.jl index 10ee400223888..b4236052db93a 100644 --- a/stdlib/LinearAlgebra/test/lu.jl +++ b/stdlib/LinearAlgebra/test/lu.jl @@ -11,7 +11,7 @@ n = 10 n1 = div(n, 2) n2 = 2*n1 -Random.seed(1234321) +Random.seed!(1234321) areal = randn(n,n)/2 aimg = randn(n,n)/2 @@ -208,7 +208,7 @@ end end @testset "conversion" begin - Random.seed(3) + Random.seed!(3) a = Tridiagonal(rand(9),rand(10),rand(9)) fa = Array(a) falu = lu(fa) diff --git a/stdlib/LinearAlgebra/test/pinv.jl b/stdlib/LinearAlgebra/test/pinv.jl index 54bf9af3a1986..3bf96b021c2bf 100644 --- a/stdlib/LinearAlgebra/test/pinv.jl +++ b/stdlib/LinearAlgebra/test/pinv.jl @@ -4,7 +4,7 @@ module TestPinv using Test, LinearAlgebra, Random -Random.seed(12345) +Random.seed!(12345) function hilb(T::Type, n::Integer) a = Matrix{T}(undef, n, n) diff --git a/stdlib/LinearAlgebra/test/qr.jl b/stdlib/LinearAlgebra/test/qr.jl index 4a721f59f4f77..28e923867f940 100644 --- a/stdlib/LinearAlgebra/test/qr.jl +++ b/stdlib/LinearAlgebra/test/qr.jl @@ -11,7 +11,7 @@ n = 10 n1 = div(n, 2) n2 = 2*n1 -Random.seed(1234321) +Random.seed!(1234321) areal = randn(n,n)/2 aimg = randn(n,n)/2 diff --git a/stdlib/LinearAlgebra/test/schur.jl b/stdlib/LinearAlgebra/test/schur.jl index 78a23fa080998..b660a0700ef95 100644 --- a/stdlib/LinearAlgebra/test/schur.jl +++ b/stdlib/LinearAlgebra/test/schur.jl @@ -11,7 +11,7 @@ n = 10 n1 = div(n, 2) n2 = 2*n1 -Random.seed(1234321) +Random.seed!(1234321) areal = randn(n,n)/2 aimg = randn(n,n)/2 diff --git a/stdlib/LinearAlgebra/test/special.jl b/stdlib/LinearAlgebra/test/special.jl index 54e1053521747..2092122c33458 100644 --- a/stdlib/LinearAlgebra/test/special.jl +++ b/stdlib/LinearAlgebra/test/special.jl @@ -6,7 +6,7 @@ using Test, LinearAlgebra, SparseArrays, Random using LinearAlgebra: rmul! n= 10 #Size of matrix to test -Random.seed(1) +Random.seed!(1) @testset "Interconversion between special matrix types" begin a = [1.0:n;] diff --git a/stdlib/LinearAlgebra/test/svd.jl b/stdlib/LinearAlgebra/test/svd.jl index 0182182e2902d..e253f6eb2b8d7 100644 --- a/stdlib/LinearAlgebra/test/svd.jl +++ b/stdlib/LinearAlgebra/test/svd.jl @@ -37,7 +37,7 @@ n = 10 n1 = div(n, 2) n2 = 2*n1 -Random.seed(1234321) +Random.seed!(1234321) areal = randn(n,n)/2 aimg = randn(n,n)/2 diff --git a/stdlib/LinearAlgebra/test/symmetric.jl b/stdlib/LinearAlgebra/test/symmetric.jl index 51ba0e73b3aa1..9e9814895d7e6 100644 --- a/stdlib/LinearAlgebra/test/symmetric.jl +++ b/stdlib/LinearAlgebra/test/symmetric.jl @@ -4,7 +4,7 @@ module TestSymmetric using Test, LinearAlgebra, SparseArrays, Random -Random.seed(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] ]) diff --git a/stdlib/LinearAlgebra/test/triangular.jl b/stdlib/LinearAlgebra/test/triangular.jl index 4156073d9a80c..de0a2470d548f 100644 --- a/stdlib/LinearAlgebra/test/triangular.jl +++ b/stdlib/LinearAlgebra/test/triangular.jl @@ -11,7 +11,7 @@ using LinearAlgebra: BlasFloat, errorbounds, full!, naivesub!, transpose!, debug && println("Triangular matrices") n = 9 -Random.seed(123) +Random.seed!(123) debug && println("Test basic type functionality") @test_throws DimensionMismatch LowerTriangular(randn(5, 4)) diff --git a/stdlib/LinearAlgebra/test/tridiag.jl b/stdlib/LinearAlgebra/test/tridiag.jl index 91999af98b11b..cb6a9799a4144 100644 --- a/stdlib/LinearAlgebra/test/tridiag.jl +++ b/stdlib/LinearAlgebra/test/tridiag.jl @@ -22,9 +22,9 @@ end @testset for elty in (Float32, Float64, ComplexF32, ComplexF64, Int) n = 12 #Size of matrix problem to test - Random.seed(123) + Random.seed!(123) if elty == Int - Random.seed(61516384) + Random.seed!(61516384) d = rand(1:100, n) dl = -rand(0:10, n-1) du = -rand(0:10, n-1) diff --git a/stdlib/LinearAlgebra/test/uniformscaling.jl b/stdlib/LinearAlgebra/test/uniformscaling.jl index 9f871f55ee232..8cadceae4a525 100644 --- a/stdlib/LinearAlgebra/test/uniformscaling.jl +++ b/stdlib/LinearAlgebra/test/uniformscaling.jl @@ -4,7 +4,7 @@ module TestUniformscaling using Test, LinearAlgebra, Random, SparseArrays -Random.seed(123) +Random.seed!(123) @testset "basic functions" begin @test I[1,1] == 1 # getindex diff --git a/stdlib/Pkg/test/NastyGenerator.jl b/stdlib/Pkg/test/NastyGenerator.jl index df2d6705a384a..16d52beee15f8 100644 --- a/stdlib/Pkg/test/NastyGenerator.jl +++ b/stdlib/Pkg/test/NastyGenerator.jl @@ -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)" - Random.seed(seed) + Random.seed!(seed) allvers = [sort(unique(randvers(k) for j = 1:q)) for i = 1:m] diff --git a/stdlib/Random/docs/src/index.md b/stdlib/Random/docs/src/index.md index 8e008e1cbd0db..c7e0c66701b0c 100644 --- a/stdlib/Random/docs/src/index.md +++ b/stdlib/Random/docs/src/index.md @@ -30,6 +30,12 @@ Additionally, normal and exponential distributions are implemented for some `Abs ## Random generation functions ```@docs +<<<<<<< 75ff13e3183a02e622d7bb72856b5304736fd394 +======= +Random.seed! +Random.MersenneTwister +Random.RandomDevice +>>>>>>> Renamed Random.seed to Random.seed! Random.rand Random.rand! Random.bitrand diff --git a/stdlib/Random/src/RNGs.jl b/stdlib/Random/src/RNGs.jl index 9652f3b1093c1..0c103305ede32 100644 --- a/stdlib/Random/src/RNGs.jl +++ b/stdlib/Random/src/RNGs.jl @@ -65,7 +65,7 @@ The entropy is obtained from the operating system. RandomDevice RandomDevice(::Nothing) = RandomDevice() -seed(rng::RandomDevice) = rng +seed!(rng::RandomDevice) = rng ## MersenneTwister @@ -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 [`seed`](@ref) function for reseeding an already existing +See the [`seed!`](@ref) function for reseeding an already existing `MersenneTwister` object. @@ -135,7 +135,7 @@ true ``` """ MersenneTwister(seed=nothing) = - Random.seed(MersenneTwister(Vector{UInt32}(), DSFMT_state()), seed) + seed!(MersenneTwister(Vector{UInt32}(), DSFMT_state()), seed) function copy!(dst::MersenneTwister, src::MersenneTwister) @@ -274,9 +274,9 @@ function make_seed(n::Integer) end end -#### seed() +#### seed!() -function seed(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) @@ -285,12 +285,12 @@ function seed(r::MersenneTwister, seed::Vector{UInt32}) return r end -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}}) = Random.seed(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 seed) +### Global RNG (must be defined after seed!) const GLOBAL_RNG = MersenneTwister(0) diff --git a/stdlib/Random/src/Random.jl b/stdlib/Random/src/Random.jl index 2b28cf2359082..cb0160f3b98f5 100644 --- a/stdlib/Random/src/Random.jl +++ b/stdlib/Random/src/Random.jl @@ -264,7 +264,7 @@ rand( ::Type{X}, d::Integer, dims::Integer...) where {X} = rand(X function __init__() try - seed() + seed!() catch ex Base.showerror_nostdio(ex, "WARNING: Error during initialization of module Random") @@ -277,7 +277,7 @@ include("normal.jl") include("misc.jl") include("deprecated.jl") -## rand & rand! & seed docstrings +## rand & rand! & seed! docstrings """ rand([rng=GLOBAL_RNG], [S], [dims...]) @@ -341,25 +341,25 @@ julia> rand!(rng, zeros(5)) rand! """ - seed([rng=GLOBAL_RNG], seed) -> rng - seed([rng=GLOBAL_RNG]) -> rng + seed!([rng=GLOBAL_RNG], seed) -> rng + seed!([rng=GLOBAL_RNG]) -> rng Reseed the random number generator: `rng` will give a reproducible sequence of numbers if and only if a `seed` is provided. Some RNGs don't accept a seed, like `RandomDevice`. -After the call to `seed`, `rng` is equivalent to a newly created +After the call to `seed!`, `rng` is equivalent to a newly created object initialized with the same seed. # Examples ```julia-repl -julia> Random.seed(1234); +julia> Random.seed!(1234); julia> x1 = rand(2) 2-element Array{Float64,1}: 0.590845 0.766797 -julia> Random.seed(1234); +julia> Random.seed!(1234); julia> x2 = rand(2) 2-element Array{Float64,1}: @@ -372,19 +372,19 @@ true julia> rng = MersenneTwister(1234); rand(rng, 2) == x1 true -julia> MersenneTwister(1) == Random.seed(rng, 1) +julia> MersenneTwister(1) == Random.seed!(rng, 1) true -julia> rand(Random.seed(rng), Bool) # not reproducible +julia> rand(Random.seed!(rng), Bool) # not reproducible true -julia> rand(Random.seed(rng), Bool) +julia> rand(Random.seed!(rng), Bool) false julia> rand(MersenneTwister(), Bool) # not reproducible either true ``` """ -seed(rng::AbstractRNG, ::Nothing) = seed(rng) +seed!(rng::AbstractRNG, ::Nothing) = seed!(rng) end # module diff --git a/stdlib/Random/src/deprecated.jl b/stdlib/Random/src/deprecated.jl index f668908b0405b..fca3cfb742c10 100644 --- a/stdlib/Random/src/deprecated.jl +++ b/stdlib/Random/src/deprecated.jl @@ -39,4 +39,4 @@ end @deprecate RandomDevice(unlimited::Bool) RandomDevice(; unlimited=unlimited) # PR #28295 -@deprecate srand Random.seed +@deprecate srand Random.seed! diff --git a/stdlib/Random/src/misc.jl b/stdlib/Random/src/misc.jl index 0676999903724..e2356f12b8d98 100644 --- a/stdlib/Random/src/misc.jl +++ b/stdlib/Random/src/misc.jl @@ -52,7 +52,7 @@ number generator, see [Random Numbers](@ref). # Examples ```jldoctest -julia> Random.seed(0); randstring() +julia> Random.seed!(0); randstring() "0IPrGg0J" julia> randstring(MersenneTwister(0), 'a':'z', 6) diff --git a/stdlib/Random/test/runtests.jl b/stdlib/Random/test/runtests.jl index b219d28aa8de5..91d3db9eb994a 100644 --- a/stdlib/Random/test/runtests.jl +++ b/stdlib/Random/test/runtests.jl @@ -15,7 +15,7 @@ using Random: Sampler, SamplerRangeFast, SamplerRangeInt, MT_CACHE_F, MT_CACHE_I import Future # randjump @testset "Issue #6573" begin - Random.seed(0) + Random.seed!(0) rand() x = rand(384) @test findall(x .== rand()) == [] @@ -131,9 +131,9 @@ for T in [UInt32, UInt64, UInt128, Int128] @test size(r) == (1, 2) @test typeof(r) == Matrix{BigInt} guardseed() do - Random.seed(0) + Random.seed!(0) r = rand(s) - Random.seed(0) + Random.seed!(0) @test rand(s) == r end end @@ -225,13 +225,13 @@ randmtzig_fill_ziggurat_tables() #same random numbers on for small ranges on all systems guardseed() do seed = rand(UInt) - Random.seed(seed) + Random.seed!(seed) r = map(Int64, rand(map(Int32, 97:122))) - Random.seed(seed) + Random.seed!(seed) @test r == rand(map(Int64, 97:122)) - Random.seed(seed) + Random.seed!(seed) r = map(UInt64, rand(map(UInt32, 97:122))) - Random.seed(seed) + Random.seed!(seed) @test r == rand(map(UInt64, 97:122)) end @@ -266,7 +266,7 @@ let mt = MersenneTwister(0) 0x4b54632b4619f4eca22675166784d229][i] end - Random.seed(mt, 0) + Random.seed!(mt, 0) for (i,T) in enumerate([Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, Float16, Float32]) A = Vector{T}(undef, 16) B = Vector{T}(undef, 31) @@ -278,7 +278,7 @@ let mt = MersenneTwister(0) 61881313582466480231846019869039259750, Float16(0.38672), 0.20027375f0][i] end - Random.seed(mt, 0) + Random.seed!(mt, 0) AF64 = Vector{Float64}(undef, Random.dsfmt_get_min_array_size()-1) @test rand!(mt, AF64)[end] == 0.957735065345398 @test rand!(mt, AF64)[end] == 0.6492481059865669 @@ -304,7 +304,7 @@ let mt = MersenneTwister(0) for A in (a, b, c) local A - Random.seed(mt, 0) + Random.seed!(mt, 0) rand(mt) # this is to fill mt.vals, cf. #9040 rand!(mt, A) # must not segfault even if Int(pointer(A)) % 16 != 0 @test A[end-4:end] == [0.3371041633752143, 0.41147647589610803, 0.6063082992397912, 0.9103565379264364, 0.16456579813368521] @@ -538,8 +538,8 @@ end # test that the following is not an error (#16925) guardseed() do - Random.seed(typemax(UInt)) - Random.seed(typemax(UInt128)) + Random.seed!(typemax(UInt)) + Random.seed!(typemax(UInt128)) end # copy, == and hash @@ -584,24 +584,24 @@ let seed = rand(UInt32, 10) # RNGs do not share their seed in randjump let r2 = Future.randjump(r, big(10)^20) @test r.seed !== r2.seed - Random.seed(r2) + Random.seed!(r2) @test seed == r.seed != r2.seed end resize!(seed, 4) @test r.seed != seed end -# Random.seed(rng, ...) returns rng (#21248) +# Random.seed!(rng, ...) returns rng (#21248) guardseed() do g = Random.GLOBAL_RNG m = MersenneTwister(0) - @test Random.seed() === g - @test Random.seed(rand(UInt)) === g - @test Random.seed(rand(UInt32, rand(1:10))) === g - @test Random.seed(m) === m - @test Random.seed(m, rand(UInt)) === m - @test Random.seed(m, rand(UInt32, rand(1:10))) === m - @test Random.seed(m, rand(1:10)) === m + @test Random.seed!() === g + @test Random.seed!(rand(UInt)) === g + @test Random.seed!(rand(UInt32, rand(1:10))) === g + @test Random.seed!(m) === m + @test Random.seed!(m, rand(UInt)) === m + @test Random.seed!(m, rand(UInt32, rand(1:10))) === m + @test Random.seed!(m, rand(1:10)) === m end # Issue 20062 - ensure internal functions reserve_1, reserve are type-stable @@ -633,7 +633,7 @@ end # this shouldn't crash (#22403) @test_throws ArgumentError rand!(Union{UInt,Int}[1, 2, 3]) -@testset "$RNG() & Random.seed(rng::$RNG) initializes randomly" for RNG in (MersenneTwister, RandomDevice) +@testset "$RNG() & Random.seed!(rng::$RNG) initializes randomly" for RNG in (MersenneTwister, RandomDevice) m = RNG() a = rand(m, Int) m = RNG() @@ -642,22 +642,22 @@ end m = RNG(nothing) b = rand(m, Int) @test b != a - Random.seed(m) + Random.seed!(m) c = rand(m, Int) @test c ∉ (a, b) - Random.seed(m) + Random.seed!(m) @test rand(m, Int) ∉ (a, b, c) - Random.seed(m, nothing) + Random.seed!(m, nothing) d = rand(m, Int) @test d ∉ (a, b, c) - Random.seed(m, nothing) + Random.seed!(m, nothing) @test rand(m, Int) ∉ (a, b, c, d) end -@testset "MersenneTwister($seed) & Random.seed(m::MersenneTwister, $seed) produce the same stream" for seed in [0:5; 10000:10005] +@testset "MersenneTwister($seed) & Random.seed!(m::MersenneTwister, $seed) produce the same stream" for seed in [0:5; 10000:10005] m = MersenneTwister(seed) a = [rand(m) for _=1:100] - Random.seed(m, seed) + Random.seed!(m, seed) @test a == [rand(m) for _=1:100] end @@ -669,7 +669,7 @@ end @testset "rand(::$(typeof(RNG)), ::UnitRange{$T}" for RNG ∈ (MersenneTwister(), RandomDevice()), T ∈ (Int32, UInt32, Int64, Int128, UInt128) - RNG isa MersenneTwister && Random.seed(RNG, rand(UInt128)) # for reproducibility + RNG isa MersenneTwister && Random.seed!(RNG, rand(UInt128)) # for reproducibility r = T(1):T(108) @test rand(RNG, SamplerRangeFast(r)) ∈ r end diff --git a/stdlib/SparseArrays/src/sparsematrix.jl b/stdlib/SparseArrays/src/sparsematrix.jl index 1a1f129a1554f..85478ca6cc831 100644 --- a/stdlib/SparseArrays/src/sparsematrix.jl +++ b/stdlib/SparseArrays/src/sparsematrix.jl @@ -1427,7 +1427,7 @@ distribution is used in case `rfn` is not specified. The optional `rng` argument specifies a random number generator, see [Random Numbers](@ref). # Examples -```jldoctest; setup = :(using Random; Random.seed(1234)) +```jldoctest; setup = :(using Random; Random.seed!(1234)) julia> sprand(Bool, 2, 2, 0.5) 2×2 SparseMatrixCSC{Bool,Int64} with 2 stored entries: [1, 1] = true @@ -1476,7 +1476,7 @@ where nonzero values are sampled from the normal distribution. The optional `rng argument specifies a random number generator, see [Random Numbers](@ref). # Examples -```jldoctest; setup = :(using Random; Random.seed(0)) +```jldoctest; setup = :(using Random; Random.seed!(0)) julia> sprandn(2, 2, 0.75) 2×2 SparseMatrixCSC{Float64,Int64} with 2 stored entries: [1, 1] = 0.586617 diff --git a/stdlib/SparseArrays/test/sparse.jl b/stdlib/SparseArrays/test/sparse.jl index 35386bcea8676..613b957738f35 100644 --- a/stdlib/SparseArrays/test/sparse.jl +++ b/stdlib/SparseArrays/test/sparse.jl @@ -1742,13 +1742,13 @@ end end @testset "sparse matrix opnormestinv" begin - Random.seed(1234) + Random.seed!(1234) Ac = sprandn(20,20,.5) + im* sprandn(20,20,.5) Aci = ceil.(Int64, 100*sprand(20,20,.5)) + im*ceil.(Int64, sprand(20,20,.5)) Ar = sprandn(20,20,.5) Ari = ceil.(Int64, 100*Ar) if Base.USE_GPL_LIBS - # NOTE: opnormestinv is probabilistic, so requires a fixed seed (set above in Random.seed(1234)) + # NOTE: opnormestinv is probabilistic, so requires a fixed seed (set above in Random.seed!(1234)) @test SparseArrays.opnormestinv(Ac,3) ≈ opnorm(inv(Array(Ac)),1) atol=1e-4 @test SparseArrays.opnormestinv(Aci,3) ≈ opnorm(inv(Array(Aci)),1) atol=1e-4 @test SparseArrays.opnormestinv(Ar) ≈ opnorm(inv(Array(Ar)),1) atol=1e-4 @@ -1795,7 +1795,7 @@ end end @testset "factorization" begin - Random.seed(123) + Random.seed!(123) local A A = sparse(Diagonal(rand(5))) + sprandn(5, 5, 0.2) + im*sprandn(5, 5, 0.2) A = A + copy(A') diff --git a/stdlib/SparseArrays/test/sparsevector.jl b/stdlib/SparseArrays/test/sparsevector.jl index 2eee394fac830..6061b21a6721c 100644 --- a/stdlib/SparseArrays/test/sparsevector.jl +++ b/stdlib/SparseArrays/test/sparsevector.jl @@ -1027,7 +1027,7 @@ end end @testset "dropzeros[!] with length=$m" for m in (10, 20, 30) - Random.seed(123) + Random.seed!(123) nzprob, targetnumposzeros, targetnumnegzeros = 0.4, 5, 5 v = sprand(m, nzprob) struczerosv = findall(x -> x == 0, v) diff --git a/stdlib/SuiteSparse/test/cholmod.jl b/stdlib/SuiteSparse/test/cholmod.jl index a439226c4cfff..aaa9e49266ca5 100644 --- a/stdlib/SuiteSparse/test/cholmod.jl +++ b/stdlib/SuiteSparse/test/cholmod.jl @@ -8,7 +8,7 @@ using Serialization using LinearAlgebra: issuccess, PosDefException # CHOLMOD tests -Random.seed(123) +Random.seed!(123) @testset "based on deps/SuiteSparse-4.0.2/CHOLMOD/Demo/" begin @@ -733,7 +733,7 @@ end end @testset "Check that Symmetric{SparseMatrixCSC} can be constructed from CHOLMOD.Sparse" begin - Int === Int32 && Random.seed(124) + Int === Int32 && Random.seed!(124) A = sprandn(10, 10, 0.1) B = CHOLMOD.Sparse(A) C = B'B diff --git a/stdlib/Test/src/Test.jl b/stdlib/Test/src/Test.jl index 20f671a1b50bf..49b0c951816ef 100644 --- a/stdlib/Test/src/Test.jl +++ b/stdlib/Test/src/Test.jl @@ -1009,7 +1009,7 @@ method, which by default will return a list of the testset objects used in each iteration. Before the execution of the body of a `@testset`, there is an implicit -call to `Random.seed(seed)` where `seed` is the current seed of the global RNG. +call to `Random.seed!(seed)` where `seed` is the current seed of the global RNG. Moreover, after the execution of the body, the state of the global RNG is restored to what it was before the `@testset`. This is meant to ease reproducibility in case of failure, and to allow seamless @@ -1077,7 +1077,7 @@ function testset_beginend(args, tests, source) oldrng = copy(GLOBAL_RNG) try # GLOBAL_RNG is re-seeded with its own seed to ease reproduce a failed test - Random.seed(GLOBAL_RNG.seed) + Random.seed!(GLOBAL_RNG.seed) $(esc(tests)) catch err err isa InterruptException && rethrow(err) @@ -1143,7 +1143,7 @@ function testset_forloop(args, testloop, source) if !first_iteration pop_testset() push!(arr, finish(ts)) - # it's 1000 times faster to copy from tmprng rather than calling Random.seed + # it's 1000 times faster to copy from tmprng rather than calling Random.seed! copy!(GLOBAL_RNG, tmprng) end @@ -1164,7 +1164,7 @@ function testset_forloop(args, testloop, source) local first_iteration = true local ts local oldrng = copy(GLOBAL_RNG) - Random.seed(GLOBAL_RNG.seed) + Random.seed!(GLOBAL_RNG.seed) local tmprng = copy(GLOBAL_RNG) try $(Expr(:for, Expr(:block, [esc(v) for v in loopvars]...), blk)) @@ -1558,10 +1558,10 @@ function guardseed(f::Function, r::AbstractRNG=GLOBAL_RNG) end end -"`guardseed(f, seed)` is equivalent to running `Random.seed(seed); f()` and +"`guardseed(f, seed)` is equivalent to running `Random.seed!(seed); f()` and then restoring the state of the global RNG as it was before." guardseed(f::Function, seed::Union{Vector{UInt32},Integer}) = guardseed() do - Random.seed(seed) + Random.seed!(seed) f() end diff --git a/stdlib/Test/test/runtests.jl b/stdlib/Test/test/runtests.jl index 5becb748eaf5e..3f2bc27e3849d 100644 --- a/stdlib/Test/test/runtests.jl +++ b/stdlib/Test/test/runtests.jl @@ -612,18 +612,18 @@ let msg = split(read(pipeline(ignorestatus(`$(Base.julia_cmd()) --startup-file=n @test msg == rstrip(msg) end -@testset "test guarded Random.seed" begin +@testset "test guarded Random.seed!" begin seed = rand(UInt) orig = copy(Random.GLOBAL_RNG) @test guardseed(()->rand(), seed) == guardseed(()->rand(), seed) @test guardseed(()->rand(Int), seed) == guardseed(()->rand(Int), seed) r1, r2 = MersenneTwister(0), MersenneTwister(0) a, b = guardseed(r1) do - Random.seed(r1, 0) + Random.seed!(r1, 0) rand(r1), rand(r1, Int) end::Tuple{Float64,Int} c, d = guardseed(r2) do - Random.seed(r2, 0) + Random.seed!(r2, 0) rand(r2), rand(r2, Int) end::Tuple{Float64,Int} @test a == c == rand(r1) == rand(r2) @@ -741,7 +741,7 @@ end @testset "@testset preserves GLOBAL_RNG's state, and re-seeds it" begin # i.e. it behaves as if it was wrapped in a `guardseed(GLOBAL_RNG.seed)` block seed = rand(UInt128) - Random.seed(seed) + Random.seed!(seed) a = rand() @testset begin # global RNG must re-seeded at the beginning of @testset @@ -752,7 +752,7 @@ end end # the @testset's above must have no consequence for rand() below b = rand() - Random.seed(seed) + Random.seed!(seed) @test a == rand() @test b == rand() end diff --git a/test/sorting.jl b/test/sorting.jl index 3759121b9ff54..4cbc7901acb8d 100644 --- a/test/sorting.jl +++ b/test/sorting.jl @@ -227,7 +227,7 @@ function randn_with_nans(n,p) end @testset "advanced sorting" begin - Random.seed(0xdeadbeef) + Random.seed!(0xdeadbeef) for n in [0:10; 100; 101; 1000; 1001] local r r = -5:5 diff --git a/test/testdefs.jl b/test/testdefs.jl index 1e59184b6485e..01f55585fead3 100644 --- a/test/testdefs.jl +++ b/test/testdefs.jl @@ -17,8 +17,8 @@ function runtests(name, path, isolate=true; seed=nothing) @eval(m, using Test, Random) ex = quote @timed @testset $"$name" begin - # Random.seed(nothing) will fail - $seed != nothing && Random.seed($seed) + # Random.seed!(nothing) will fail + $seed != nothing && Random.seed!($seed) include($"$path.jl") end end From d3142e0031583e7025d1bbb28ffcb1e6b3d435b1 Mon Sep 17 00:00:00 2001 From: Olivier Thill Date: Fri, 27 Jul 2018 21:25:25 +0100 Subject: [PATCH 16/16] Fix Merge Issue. --- stdlib/Random/docs/src/index.md | 6 ------ 1 file changed, 6 deletions(-) diff --git a/stdlib/Random/docs/src/index.md b/stdlib/Random/docs/src/index.md index c7e0c66701b0c..8e008e1cbd0db 100644 --- a/stdlib/Random/docs/src/index.md +++ b/stdlib/Random/docs/src/index.md @@ -30,12 +30,6 @@ Additionally, normal and exponential distributions are implemented for some `Abs ## Random generation functions ```@docs -<<<<<<< 75ff13e3183a02e622d7bb72856b5304736fd394 -======= -Random.seed! -Random.MersenneTwister -Random.RandomDevice ->>>>>>> Renamed Random.seed to Random.seed! Random.rand Random.rand! Random.bitrand