diff --git a/NEWS.md b/NEWS.md index 3cde88143e8e2..08a72768fe363 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!` ([#27726]). + * `realmin`/`realmax` are deprecated in favor of `floatmin`/`floatmax` ([#28302]). Command-line option changes @@ -1674,6 +1676,7 @@ Command-line option changes [#27635]: https://github.com/JuliaLang/julia/issues/27635 [#27641]: https://github.com/JuliaLang/julia/issues/27641 [#27711]: https://github.com/JuliaLang/julia/issues/27711 +[#27726]: https://github.com/JuliaLang/julia/issues/27726 [#27746]: https://github.com/JuliaLang/julia/issues/27746 [#27859]: https://github.com/JuliaLang/julia/issues/27859 [#27908]: https://github.com/JuliaLang/julia/issues/27908 diff --git a/doc/src/devdocs/subarrays.md b/doc/src/devdocs/subarrays.md index 657d8a8909998..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.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..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> 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..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; 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 diff --git a/stdlib/LinearAlgebra/test/bidiag.jl b/stdlib/LinearAlgebra/test/bidiag.jl index f90a13caddaa3..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 -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..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 -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..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 -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..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 - 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..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 -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..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 -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..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 -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..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)) -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..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 - 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..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 - 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..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 -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..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 -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 e0253da4a51c8..9be74730127aa 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..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 -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..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 -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..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 -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..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 -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..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 -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..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 -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..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 - 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..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 -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..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)" - srand(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 a26e0bb096c46..8e008e1cbd0db 100644 --- a/stdlib/Random/docs/src/index.md +++ b/stdlib/Random/docs/src/index.md @@ -56,7 +56,7 @@ Random.shuffle! ## Generators (creation and seeding) ```@docs -Random.srand +Random.seed! Random.MersenneTwister Random.RandomDevice ``` diff --git a/stdlib/Random/src/RNGs.jl b/stdlib/Random/src/RNGs.jl index bfe357dd4e7b0..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() -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..cb0160f3b98f5 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, @@ -265,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") @@ -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> Random.seed!(1234); julia> x1 = rand(2) 2-element Array{Float64,1}: 0.590845 0.766797 -julia> srand(1234); +julia> Random.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) == Random.seed!(rng, 1) true -julia> rand(srand(rng), Bool) # not reproducible +julia> rand(Random.seed!(rng), Bool) # not reproducible true -julia> rand(srand(rng), Bool) +julia> rand(Random.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 diff --git a/stdlib/Random/src/deprecated.jl b/stdlib/Random/src/deprecated.jl index bce29665d2c7e..fca3cfb742c10 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 #28295 +@deprecate srand Random.seed! diff --git a/stdlib/Random/src/misc.jl b/stdlib/Random/src/misc.jl index 367163788d516..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> srand(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 859b320ebd99a..91d3db9eb994a 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")) @@ -15,7 +15,7 @@ using Random: Sampler, SamplerRangeFast, SamplerRangeInt, MT_CACHE_F, MT_CACHE_I import Future # randjump @testset "Issue #6573" begin - srand(0) + Random.seed!(0) rand() x = rand(384) @test findall(x .== rand()) == [] @@ -130,10 +130,10 @@ for T in [UInt32, UInt64, UInt128, Int128] r = rand(s, 1, 2) @test size(r) == (1, 2) @test typeof(r) == Matrix{BigInt} - guardsrand() do - srand(0) + guardseed() do + Random.seed!(0) r = rand(s) - srand(0) + Random.seed!(0) @test rand(s) == r end end @@ -223,15 +223,15 @@ 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) - srand(seed) + Random.seed!(seed) r = map(Int64, rand(map(Int32, 97:122))) - srand(seed) + Random.seed!(seed) @test r == rand(map(Int64, 97:122)) - srand(seed) + Random.seed!(seed) r = map(UInt64, rand(map(UInt32, 97:122))) - srand(seed) + Random.seed!(seed) @test r == rand(map(UInt64, 97:122)) end @@ -266,7 +266,7 @@ let mt = MersenneTwister(0) 0x4b54632b4619f4eca22675166784d229][i] end - srand(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 - srand(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 - srand(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] @@ -537,9 +537,9 @@ end end # test that the following is not an error (#16925) -guardsrand() do - srand(typemax(UInt)) - srand(typemax(UInt128)) +guardseed() do + 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 - srand(r2) + Random.seed!(r2) @test seed == r.seed != r2.seed end resize!(seed, 4) @test r.seed != seed end -# srand(rng, ...) returns rng (#21248) -guardsrand() do +# Random.seed!(rng, ...) returns rng (#21248) +guardseed() 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 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() & srand(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 - srand(m) + Random.seed!(m) c = rand(m, Int) @test c ∉ (a, b) - srand(m) + Random.seed!(m) @test rand(m, Int) ∉ (a, b, c) - srand(m, nothing) + Random.seed!(m, nothing) d = rand(m, Int) @test d ∉ (a, b, c) - srand(m, nothing) + Random.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) & 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] - srand(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 && srand(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 09298386dd2ee..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; 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..613b957738f35 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] @@ -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') @@ -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/SparseArrays/test/sparsevector.jl b/stdlib/SparseArrays/test/sparsevector.jl index c633cc444c0ba..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) - 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/Statistics/test/runtests.jl b/stdlib/Statistics/test/runtests.jl index 3ec105eb98719..8cd4129e9bbbd 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/SuiteSparse/test/cholmod.jl b/stdlib/SuiteSparse/test/cholmod.jl index bf255e145b0ab..aaa9e49266ca5 100644 --- a/stdlib/SuiteSparse/test/cholmod.jl +++ b/stdlib/SuiteSparse/test/cholmod.jl @@ -3,11 +3,12 @@ using SuiteSparse.CHOLMOD using DelimitedFiles using Test +using Random 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 +733,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..49b0c951816ef 100644 --- a/stdlib/Test/src/Test.jl +++ b/stdlib/Test/src/Test.jl @@ -28,7 +28,8 @@ export TestSetException import Distributed: myid -using Random: srand, AbstractRNG, GLOBAL_RNG +using Random +using Random: AbstractRNG, GLOBAL_RNG using InteractiveUtils: gen_call_with_extracted_types #----------------------------------------------------------------------- @@ -1008,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 `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 @@ -1070,13 +1071,13 @@ 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) 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 +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 srand + # it's 1000 times faster to copy from tmprng rather than calling Random.seed! copy!(GLOBAL_RNG, tmprng) end @@ -1163,7 +1164,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)) @@ -1546,9 +1547,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,10 +1558,10 @@ function guardsrand(f::Function, r::AbstractRNG=GLOBAL_RNG) end end -"`guardsrand(f, seed)` is equivalent to running `srand(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 - srand(seed) +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 b509963a8957f..3f2bc27e3849d 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 @@ -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) + @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 - srand(r1, 0) + a, b = guardseed(r1) do + Random.seed!(r1, 0) rand(r1), rand(r1, Int) end::Tuple{Float64,Int} - c, d = guardsrand(r2) do - srand(r2, 0) + c, d = guardseed(r2) do + Random.seed!(r2, 0) rand(r2), rand(r2, Int) end::Tuple{Float64,Int} @test a == c == rand(r1) == rand(r2) @@ -739,9 +739,9 @@ 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) - 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 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 diff --git a/test/sorting.jl b/test/sorting.jl index cfc6e86b9ae86..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 - 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..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 - # srand(nothing) will fail - $seed != nothing && srand($seed) + # Random.seed!(nothing) will fail + $seed != nothing && Random.seed!($seed) include($"$path.jl") end end