diff --git a/base/deprecated.jl b/base/deprecated.jl index 221141ffe881f..b359a11473b38 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -199,20 +199,6 @@ end import .LinAlg: cond @deprecate cond(F::LinAlg.LU, p::Integer) cond(convert(AbstractArray, F), p) -# PR #21359 -import .Random: srand, randjump - -@deprecate srand(r::MersenneTwister, filename::AbstractString, n::Integer=4) srand(r, read!(filename, Vector{UInt32}(uninitialized, Int(n)))) -@deprecate srand(filename::AbstractString, n::Integer=4) srand(read!(filename, Vector{UInt32}(uninitialized, Int(n)))) -@deprecate MersenneTwister(filename::AbstractString) srand(MersenneTwister(0), read!(filename, Vector{UInt32}(uninitialized, Int(4)))) - -function randjump(mt::MersenneTwister, jumps::Integer, jumppoly::AbstractString) - depwarn("`randjump(rng, jumps, jumppoly::AbstractString)` is deprecated; use `randjump(rng, steps, jumps)` instead", :randjump) - Base.Random._randjump(mt, dSFMT.GF2X(jumppoly), jumps) -end - -@deprecate randjump(mt::MersenneTwister, jumps::Integer) randjump(mt, big(10)^20, jumps) - # PR #21974 @deprecate versioninfo(verbose::Bool) versioninfo(verbose=verbose) @deprecate versioninfo(io::IO, verbose::Bool) versioninfo(io, verbose=verbose) @@ -641,8 +627,6 @@ end @deprecate convert(::Type{S}, g::Unicode.GraphemeIterator) where {S<:AbstractString} convert(S, g.s) @deprecate convert(::Type{String}, v::AbstractVector{Char}) String(v) -@deprecate convert(::Type{UInt128}, u::Random.UUID) UInt128(u) -@deprecate convert(::Type{Random.UUID}, s::AbstractString) Random.UUID(s) @deprecate convert(::Type{Libc.FILE}, s::IO) Libc.FILE(s) @deprecate convert(::Type{VersionNumber}, v::Integer) VersionNumber(v) @deprecate convert(::Type{VersionNumber}, v::Tuple) VersionNumber(v) @@ -2338,6 +2322,28 @@ end # issue #24822 @deprecate_binding Display AbstractDisplay +# PR #24874 +@deprecate_moved rand! "Random" true true +@deprecate_moved srand "Random" true true +@deprecate_moved AbstractRNG "Random" true true +@deprecate_moved randcycle "Random" true true +@deprecate_moved randcycle! "Random" true true +@deprecate_moved randperm "Random" true true +@deprecate_moved randperm! "Random" true true +@deprecate_moved shuffle "Random" true true +@deprecate_moved shuffle! "Random" true true +@deprecate_moved randsubseq "Random" true true +@deprecate_moved randsubseq! "Random" true true +@deprecate_moved randstring "Random" true true +@deprecate_moved MersenneTwister "Random" true true +@deprecate_moved RandomDevice "Random" true true +@deprecate_moved randn! "Random" true true +@deprecate_moved randexp "Random" true true +@deprecate_moved randexp! "Random" true true +@deprecate_moved bitrand "Random" true true +@deprecate_moved randjump "Random" true true +@deprecate_moved GLOBAL_RNG "Random" false true + # 24595 @deprecate falses(A::AbstractArray) falses(size(A)) @deprecate trues(A::AbstractArray) trues(size(A)) diff --git a/base/error.jl b/base/error.jl index 1edf3bf5ee4ef..2065f71389d64 100644 --- a/base/error.jl +++ b/base/error.jl @@ -172,7 +172,7 @@ start(ebo::ExponentialBackOff) = (ebo.n, min(ebo.first_delay, ebo.max_delay)) function next(ebo::ExponentialBackOff, state) next_n = state[1]-1 curr_delay = state[2] - next_delay = min(ebo.max_delay, state[2] * ebo.factor * (1.0 - ebo.jitter + (rand() * 2.0 * ebo.jitter))) + next_delay = min(ebo.max_delay, state[2] * ebo.factor * (1.0 - ebo.jitter + (rand(Float64) * 2.0 * ebo.jitter))) (curr_delay, (next_n, next_delay)) end done(ebo::ExponentialBackOff, state) = state[1]<1 diff --git a/base/exports.jl b/base/exports.jl index b23e84c77cf3a..c2751feb64997 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -446,12 +446,6 @@ export prod!, prod, promote_shape, - randcycle, - randcycle!, - randperm, - randperm!, - randsubseq!, - randsubseq, range, reducedim, repmat, @@ -723,7 +717,6 @@ export print_shortest, print_with_color, println, - randstring, repeat, replace, replace!, @@ -755,20 +748,6 @@ export @warn, @error, -# random numbers - AbstractRNG, - MersenneTwister, - RandomDevice, - rand!, - rand, - randn!, - randn, - randexp!, - randexp, - srand, - bitrand, - randjump, - # bigfloat & precision precision, rounding, @@ -1117,6 +1096,10 @@ export unsafe_store!, unsafe_write, +# implemented in Random module + rand, + randn, + # Macros # parser internal @__FILE__, diff --git a/base/file.jl b/base/file.jl index 122c35f485a13..dcc86827e7ef1 100644 --- a/base/file.jl +++ b/base/file.jl @@ -285,7 +285,7 @@ function mktemp(parent=tempdir()) end function mktempdir(parent=tempdir()) - seed::UInt32 = rand(UInt32) + seed::UInt32 = Base.Crand(UInt32) while true if (seed & typemax(UInt16)) == 0 seed += 1 diff --git a/base/serialize.jl b/base/serialize.jl index 6ef393c4157bf..fedb64fc5d6c2 100644 --- a/base/serialize.jl +++ b/base/serialize.jl @@ -1195,14 +1195,4 @@ function deserialize(s::AbstractSerializer, t::Type{Regex}) Regex(pattern, compile_options, match_options) end -if !Sys.iswindows() - function serialize(s::AbstractSerializer, rd::RandomDevice) - serialize_type(s, typeof(rd)) - serialize(s, rd.unlimited) - end - function deserialize(s::AbstractSerializer, t::Type{RandomDevice}) - unlimited = deserialize(s) - return RandomDevice(unlimited) - end -end end diff --git a/base/sysimg.jl b/base/sysimg.jl index f6cec014e995a..066457c4a0052 100644 --- a/base/sysimg.jl +++ b/base/sysimg.jl @@ -311,6 +311,31 @@ include("lock.jl") include("threads.jl") include("weakkeydict.jl") +# To limit dependency on rand functionality (implemented in the Random +# module), Crand is used in file.jl, and could be used in error.jl +# (but it breaks a test) +""" + Crand([T::Type]) + +Interface to the C `rand()` function. If `T` is provided, generate a value of type `T` +by composing two calls to `Crand()`. `T` can be `UInt32` or `Float64`. +""" +Crand() = ccall(:rand, Cuint, ()) +# RAND_MAX at least 2^15-1 in theory, but we assume 2^16-1 (in practice, it's 2^31-1) +Crand(::Type{UInt32}) = ((Crand() % UInt32) << 16) ⊻ (Crand() % UInt32) +Crand(::Type{Float64}) = Crand(UInt32) / 2^32 + +""" + Csrand([seed]) + +Interface the the C `srand(seed)` function. +""" +Csrand(seed=floor(time())) = ccall(:srand, Cvoid, (Cuint,), seed) + +# functions defined in Random +function rand end +function randn end + # I/O include("stream.jl") include("socket.jl") @@ -373,12 +398,6 @@ include("irrationals.jl") include("mathconstants.jl") using .MathConstants: ℯ, π, pi -# random number generation -include("random/dSFMT.jl") -include("random/random.jl") -using .Random -import .Random: rand, rand! - # (s)printf macros include("printf.jl") # import .Printf @@ -471,6 +490,8 @@ using .Docs, .Markdown isdefined(Core, :Inference) && Docs.loaddocs(Core.Inference.CoreDocs.DOCS) function __init__() + # for the few uses of Crand in Base: + Csrand() # Base library init reinit_stdio() global_logger(root_module(:Logging).ConsoleLogger(STDERR)) @@ -494,20 +515,21 @@ Base.require(:Base64) Base.require(:CRC32c) Base.require(:Dates) Base.require(:DelimitedFiles) +Base.require(:Distributed) Base.require(:FileWatching) -Base.require(:Logging) +Base.require(:Future) Base.require(:IterativeEigensolvers) +Base.require(:Libdl) +Base.require(:Logging) Base.require(:Mmap) +Base.require(:Printf) Base.require(:Profile) +Base.require(:Random) Base.require(:SharedArrays) Base.require(:SparseArrays) Base.require(:SuiteSparse) Base.require(:Test) Base.require(:Unicode) -Base.require(:Distributed) -Base.require(:Printf) -Base.require(:Future) -Base.require(:Libdl) @eval Base begin @deprecate_binding Test root_module(:Test) true ", run `using Test` instead" @@ -515,6 +537,7 @@ Base.require(:Libdl) @deprecate_binding Profile root_module(:Profile) true ", run `using Profile` instead" @deprecate_binding Dates root_module(:Dates) true ", run `using Dates` instead" @deprecate_binding Distributed root_module(:Distributed) true ", run `using Distributed` instead" + @deprecate_binding Random root_module(:Random) true ", run `using Random` instead" # PR #25249 @deprecate_binding SparseArrays root_module(:SparseArrays) true ", run `using SparseArrays` instead" diff --git a/doc/src/base/arrays.md b/doc/src/base/arrays.md index b2a1074ab520d..6ab68431d73a9 100644 --- a/doc/src/base/arrays.md +++ b/doc/src/base/arrays.md @@ -34,8 +34,6 @@ Base.similar(::AbstractArray) Base.similar(::Any, ::Tuple) Base.linspace Base.logspace -Base.Random.randsubseq -Base.Random.randsubseq! ``` ## Basic functions @@ -164,16 +162,10 @@ Base.mapslices ## Combinatorics ```@docs -Base.Random.randperm -Base.Random.randperm! Base.invperm Base.isperm Base.permute!(::Any, ::AbstractVector) Base.invpermute! -Base.Random.randcycle -Base.Random.randcycle! -Base.Random.shuffle -Base.Random.shuffle! Base.reverse Base.reverseind Base.reverse! diff --git a/doc/src/base/index.md b/doc/src/base/index.md index 7dc8a9fff2879..7e19cb5213295 100644 --- a/doc/src/base/index.md +++ b/doc/src/base/index.md @@ -34,3 +34,4 @@ * [Sparse Arrays](@ref) * [Iterative Eigensolvers](@ref lib-itereigen) * [Printf](@ref) + * [Random Numbers](@ref) diff --git a/doc/src/base/numbers.md b/doc/src/base/numbers.md index 9ed0821042869..4be2570374488 100644 --- a/doc/src/base/numbers.md +++ b/doc/src/base/numbers.md @@ -130,38 +130,3 @@ BigFloat(x::Union{Integer, AbstractFloat, String}, rounding::RoundingMode) Base.MPFR.BigFloat(x, prec::Int, rounding::RoundingMode) Base.MPFR.BigFloat(x::String) ``` - -## Random Numbers - -Random number generation in Julia uses the [Mersenne Twister library](http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/#dSFMT) -via `MersenneTwister` objects. Julia has a global RNG, which is used by default. Other RNG types -can be plugged in by inheriting the `AbstractRNG` type; they can then be used to have multiple -streams of random numbers. Besides `MersenneTwister`, Julia also provides the `RandomDevice` RNG -type, which is a wrapper over the OS provided entropy. - -Most functions related to random generation accept an optional `AbstractRNG` as the first argument, -`rng` , which defaults to the global one if not provided. Morever, some of them accept optionally -dimension specifications `dims...` (which can be given as a tuple) to generate arrays of random -values. - -A `MersenneTwister` or `RandomDevice` RNG can generate random numbers of the following types: -[`Float16`](@ref), [`Float32`](@ref), [`Float64`](@ref), [`BigFloat`](@ref), [`Bool`](@ref), -[`Int8`](@ref), [`UInt8`](@ref), [`Int16`](@ref), [`UInt16`](@ref), [`Int32`](@ref), -[`UInt32`](@ref), [`Int64`](@ref), [`UInt64`](@ref), [`Int128`](@ref), [`UInt128`](@ref), -[`BigInt`](@ref) (or complex numbers of those types). -Random floating point numbers are generated uniformly in ``[0, 1)``. As `BigInt` represents -unbounded integers, the interval must be specified (e.g. `rand(big(1:6))`). - -```@docs -Base.Random.srand -Base.Random.MersenneTwister -Base.Random.RandomDevice -Base.Random.rand -Base.Random.rand! -Base.Random.bitrand -Base.Random.randn -Base.Random.randn! -Base.Random.randexp -Base.Random.randexp! -Base.Random.randjump -``` diff --git a/doc/src/base/strings.md b/doc/src/base/strings.md index e6c3e7b284c99..011cf245dfb5a 100644 --- a/doc/src/base/strings.md +++ b/doc/src/base/strings.md @@ -59,7 +59,6 @@ Base.chomp Base.thisind Base.nextind Base.prevind -Base.Random.randstring Base.textwidth Base.isalpha Base.isascii diff --git a/doc/src/index.md b/doc/src/index.md index 7660ea5456c95..7e2882ee8a624 100644 --- a/doc/src/index.md +++ b/doc/src/index.md @@ -85,14 +85,15 @@ Please read the [release notes](NEWS.md) to see what has changed since the last * [Delimited Files](@ref) * [Distributed Computing](@ref) * [File Events](@ref lib-filewatching) - * [Sparse Arrays](@ref) * [Iterative Eigensolvers](@ref lib-itereigen) * [Memory-mapped I/O](@ref) * [Printf](@ref) * [Profiling](@ref lib-profiling) + * [Random Numbers](@ref) * [Shared Arrays](@ref) - * [Unit Testing](@ref) + * [Sparse Arrays](@ref) * [Unicode](@ref) + * [Unit Testing](@ref) ## Developer Documentation diff --git a/stdlib/Base64/test/runtests.jl b/stdlib/Base64/test/runtests.jl index 8eab331ee6b1b..405ccb830bb67 100644 --- a/stdlib/Base64/test/runtests.jl +++ b/stdlib/Base64/test/runtests.jl @@ -1,6 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -using Test +using Test, Random import Base64: Base64EncodePipe, base64encode, diff --git a/stdlib/CRC32c/test/runtests.jl b/stdlib/CRC32c/test/runtests.jl index 4f0fefe93c747..d672514496f18 100644 --- a/stdlib/CRC32c/test/runtests.jl +++ b/stdlib/CRC32c/test/runtests.jl @@ -1,4 +1,4 @@ -using Test +using Test, Random using CRC32c function test_crc32c(crc32c) @@ -60,4 +60,3 @@ end crc32c_sw(io::IO, crc::UInt32=0x00000000) = crc32c_sw(io, typemax(Int64), crc) test_crc32c(crc32c) test_crc32c(crc32c_sw) - diff --git a/stdlib/DelimitedFiles/test/runtests.jl b/stdlib/DelimitedFiles/test/runtests.jl index a1731ee1c82a3..de45de722028a 100644 --- a/stdlib/DelimitedFiles/test/runtests.jl +++ b/stdlib/DelimitedFiles/test/runtests.jl @@ -1,6 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -using Test +using Test, Random using DelimitedFiles isequaldlm(m1, m2, t) = isequal(m1, m2) && (eltype(m1) == eltype(m2) == t) diff --git a/stdlib/Distributed/src/cluster.jl b/stdlib/Distributed/src/cluster.jl index fea3fb1ea3496..12727f0584ac4 100644 --- a/stdlib/Distributed/src/cluster.jl +++ b/stdlib/Distributed/src/cluster.jl @@ -1106,6 +1106,8 @@ function init_bind_addr() LPROC.bind_port = UInt16(bind_port) end +using Random: randstring + function init_parallel() start_gc_msgs_task() atexit(terminate_all_workers) diff --git a/stdlib/Distributed/test/distributed_exec.jl b/stdlib/Distributed/test/distributed_exec.jl index 18d886a2647e1..622eea4090784 100644 --- a/stdlib/Distributed/test/distributed_exec.jl +++ b/stdlib/Distributed/test/distributed_exec.jl @@ -1,6 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -using Test, Distributed +using Test, Distributed, Random import Distributed: launch, manage include(joinpath(Sys.BINDIR, "..", "share", "julia", "test", "testenv.jl")) @@ -18,7 +18,7 @@ include(joinpath(Sys.BINDIR, "..", "share", "julia", "test", "testenv.jl")) addprocs_with_testenv(4) @test nprocs() == 5 -@everywhere using Test +@everywhere using Test, Random id_me = myid() id_other = filter(x -> x != id_me, procs())[rand(1:(nprocs()-1))] @@ -1498,4 +1498,3 @@ end # cluster at any time only supports a single topology. rmprocs(workers()) include("topology.jl") - diff --git a/stdlib/Distributed/test/topology.jl b/stdlib/Distributed/test/topology.jl index 2de3791bfcf4a..2a659931ed306 100644 --- a/stdlib/Distributed/test/topology.jl +++ b/stdlib/Distributed/test/topology.jl @@ -1,5 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license +using Random + pids = addprocs_with_testenv(4; topology="master_worker") let p1 = pids[1], p2 = pids[2] diff --git a/stdlib/Future/src/Future.jl b/stdlib/Future/src/Future.jl index 76edfa8fcb588..1f4f8d98b8ed1 100644 --- a/stdlib/Future/src/Future.jl +++ b/stdlib/Future/src/Future.jl @@ -4,6 +4,8 @@ which will replace the current version in a future release of Julia." module Future +using Random + ## copy! """ @@ -37,6 +39,6 @@ For each different value of `steps`, a large polynomial has to be generated inte One is already pre-computed for `steps=big(10)^20`. """ randjump(r::MersenneTwister, steps::Integer) = - Base.Random._randjump(r, Base.Random.dSFMT.calc_jump(steps)) + Random._randjump(r, Random.dSFMT.calc_jump(steps)) end # module Future diff --git a/stdlib/IterativeEigensolvers/test/runtests.jl b/stdlib/IterativeEigensolvers/test/runtests.jl index 6f9516d295f24..c61d875aeadb4 100644 --- a/stdlib/IterativeEigensolvers/test/runtests.jl +++ b/stdlib/IterativeEigensolvers/test/runtests.jl @@ -1,7 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license using IterativeEigensolvers -using Test, SparseArrays +using Test, SparseArrays, Random @testset "eigs" begin srand(1234) diff --git a/stdlib/Mmap/test/runtests.jl b/stdlib/Mmap/test/runtests.jl index ee77f392f4c72..7ca20e7aa42b3 100644 --- a/stdlib/Mmap/test/runtests.jl +++ b/stdlib/Mmap/test/runtests.jl @@ -1,6 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -using Test, Mmap +using Test, Mmap, Random file = tempname() write(file, "Hello World\n") diff --git a/stdlib/Random/docs/src/index.md b/stdlib/Random/docs/src/index.md new file mode 100644 index 0000000000000..0ef892b0a3a96 --- /dev/null +++ b/stdlib/Random/docs/src/index.md @@ -0,0 +1,43 @@ +# Random Numbers + +Random number generation in Julia uses the [Mersenne Twister library](http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/#dSFMT) +via `MersenneTwister` objects. Julia has a global RNG, which is used by default. Other RNG types +can be plugged in by inheriting the `AbstractRNG` type; they can then be used to have multiple +streams of random numbers. Besides `MersenneTwister`, Julia also provides the `RandomDevice` RNG +type, which is a wrapper over the OS provided entropy. + +Most functions related to random generation accept an optional `AbstractRNG` as the first argument, +`rng` , which defaults to the global one if not provided. Morever, some of them accept optionally +dimension specifications `dims...` (which can be given as a tuple) to generate arrays of random +values. + +A `MersenneTwister` or `RandomDevice` RNG can generate random numbers of the following types: +[`Float16`](@ref), [`Float32`](@ref), [`Float64`](@ref), [`BigFloat`](@ref), [`Bool`](@ref), +[`Int8`](@ref), [`UInt8`](@ref), [`Int16`](@ref), [`UInt16`](@ref), [`Int32`](@ref), +[`UInt32`](@ref), [`Int64`](@ref), [`UInt64`](@ref), [`Int128`](@ref), [`UInt128`](@ref), +[`BigInt`](@ref) (or complex numbers of those types). +Random floating point numbers are generated uniformly in ``[0, 1)``. As `BigInt` represents +unbounded integers, the interval must be specified (e.g. `rand(big(1:6))`). + +```@docs +Random.srand +Random.MersenneTwister +Random.RandomDevice +Random.rand +Random.rand! +Random.bitrand +Random.randn +Random.randn! +Random.randexp +Random.randexp! +Random.randjump +Random.randstring +Random.randsubseq +Random.randsubseq! +Random.randperm +Random.randperm! +Random.randcycle +Random.randcycle! +Random.shuffle +Random.shuffle! +``` diff --git a/base/random/RNGs.jl b/stdlib/Random/src/RNGs.jl similarity index 98% rename from base/random/RNGs.jl rename to stdlib/Random/src/RNGs.jl index 2a46033531379..9c802bef750e8 100644 --- a/base/random/RNGs.jl +++ b/stdlib/Random/src/RNGs.jl @@ -27,6 +27,16 @@ else # !windows end rand(rd::RandomDevice, sp::SamplerBoolBitInteger) = read( rd.file, sp[]) + + function serialize(s::AbstractSerializer, rd::RandomDevice) + Serializer.serialize_type(s, typeof(rd)) + serialize(s, rd.unlimited) + end + function deserialize(s::AbstractSerializer, t::Type{RandomDevice}) + unlimited = deserialize(s) + return RandomDevice(unlimited) + end + end # os-test # NOTE: this can't be put within the if-else block above diff --git a/base/random/random.jl b/stdlib/Random/src/Random.jl similarity index 97% rename from base/random/random.jl rename to stdlib/Random/src/Random.jl index fcbe1fe8b659a..7ce0802274353 100644 --- a/base/random/random.jl +++ b/stdlib/Random/src/Random.jl @@ -1,17 +1,22 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license +__precompile__(true) + module Random -using Base.dSFMT +include("dSFMT.jl") + +using .dSFMT using Base.GMP.MPZ using Base.GMP: Limb + using Base: BitInteger, BitInteger_types, BitUnsigned, @gc_preserve -import Base: copymutable, copy, copy!, ==, hash +import Base: copymutable, copy, copy!, ==, hash, serialize, deserialize, convert +import Base: rand, randn export srand, - rand, rand!, - randn, randn!, + rand!, randn!, randexp, randexp!, bitrand, randstring, @@ -20,13 +25,15 @@ export srand, randperm, randperm!, randcycle, randcycle!, AbstractRNG, MersenneTwister, RandomDevice, - GLOBAL_RNG, randjump + defaultRNG, randjump ## general definitions abstract type AbstractRNG end +defaultRNG() = GLOBAL_RNG + ### integers @@ -65,6 +72,7 @@ for UI = (:UInt10, :UInt10Raw, :UInt23, :UInt23Raw, :UInt52, :UInt52Raw, end end + ### floats abstract type FloatInterval{T<:AbstractFloat} end @@ -248,7 +256,7 @@ include("RNGs.jl") include("generation.jl") include("normal.jl") include("misc.jl") - +include("deprecated.jl") ## rand & rand! & srand docstrings diff --git a/base/random/dSFMT.jl b/stdlib/Random/src/dSFMT.jl similarity index 100% rename from base/random/dSFMT.jl rename to stdlib/Random/src/dSFMT.jl diff --git a/stdlib/Random/src/deprecated.jl b/stdlib/Random/src/deprecated.jl new file mode 100644 index 0000000000000..1f6883275cdf1 --- /dev/null +++ b/stdlib/Random/src/deprecated.jl @@ -0,0 +1,17 @@ +# This file is a part of Julia. License is MIT: https://julialang.org/license + +# PR #21359 + +@deprecate srand(r::MersenneTwister, filename::AbstractString, n::Integer=4) srand(r, read!(filename, Vector{UInt32}(uninitialized, Int(n)))) +@deprecate srand(filename::AbstractString, n::Integer=4) srand(read!(filename, Vector{UInt32}(uninitialized, Int(n)))) +@deprecate MersenneTwister(filename::AbstractString) srand(MersenneTwister(0), read!(filename, Vector{UInt32}(uninitialized, Int(4)))) + +function randjump(mt::MersenneTwister, jumps::Integer, jumppoly::AbstractString) + depwarn("`randjump(rng, jumps, jumppoly::AbstractString)` is deprecated; use `randjump(rng, steps, jumps)` instead", :randjump) + Base.Random._randjump(mt, dSFMT.GF2X(jumppoly), jumps) +end + +@deprecate randjump(mt::MersenneTwister, jumps::Integer) randjump(mt, big(10)^20, jumps) + +@deprecate convert(::Type{UInt128}, u::UUID) UInt128(u) +@deprecate convert(::Type{UUID}, s::AbstractString) UUID(s) diff --git a/base/random/generation.jl b/stdlib/Random/src/generation.jl similarity index 100% rename from base/random/generation.jl rename to stdlib/Random/src/generation.jl diff --git a/base/random/misc.jl b/stdlib/Random/src/misc.jl similarity index 99% rename from base/random/misc.jl rename to stdlib/Random/src/misc.jl index e523718f1922d..fc9980cdc1593 100644 --- a/base/random/misc.jl +++ b/stdlib/Random/src/misc.jl @@ -362,7 +362,7 @@ according to section 4.5 of the RFC. ```jldoctest julia> rng = MersenneTwister(1234); -julia> Base.Random.uuid1(rng) +julia> Random.uuid1(rng) 2cc938da-5937-11e7-196e-0f4ef71aa64b ``` """ @@ -399,7 +399,7 @@ as specified by RFC 4122. ```jldoctest julia> rng = MersenneTwister(1234); -julia> Base.Random.uuid4(rng) +julia> Random.uuid4(rng) 82015f10-44cc-4827-996e-0f4ef71aa64b ``` """ @@ -419,7 +419,7 @@ Inspects the given UUID and returns its version (see RFC 4122). ```jldoctest julia> rng = MersenneTwister(1234); -julia> Base.Random.uuid_version(Base.Random.uuid4(rng)) +julia> Random.uuid_version(Random.uuid4(rng)) 4 ``` """ diff --git a/base/random/normal.jl b/stdlib/Random/src/normal.jl similarity index 100% rename from base/random/normal.jl rename to stdlib/Random/src/normal.jl diff --git a/stdlib/Random/test/OAs.jl b/stdlib/Random/test/OAs.jl new file mode 100644 index 0000000000000..0d0b64041f1b6 --- /dev/null +++ b/stdlib/Random/test/OAs.jl @@ -0,0 +1,124 @@ +# This file is a part of Julia. License is MIT: https://julialang.org/license + +# copied verbatim from julia/test/TestHelpers.jl + +module OAs + +using Base: Indices, IndexCartesian, IndexLinear, tail + +export OffsetArray + +struct OffsetArray{T,N,AA<:AbstractArray} <: AbstractArray{T,N} + parent::AA + offsets::NTuple{N,Int} +end +OffsetVector{T,AA<:AbstractArray} = OffsetArray{T,1,AA} + +OffsetArray(A::AbstractArray{T,N}, offsets::NTuple{N,Int}) where {T,N} = OffsetArray{T,N,typeof(A)}(A, offsets) +OffsetArray(A::AbstractArray{T,N}, offsets::Vararg{Int,N}) where {T,N} = OffsetArray(A, offsets) + +OffsetArray{T,N}(::Uninitialized, inds::Indices{N}) where {T,N} = + OffsetArray{T,N,Array{T,N}}(Array{T,N}(uninitialized, map(length, inds)), map(indsoffset, inds)) +OffsetArray{T}(::Uninitialized, inds::Indices{N}) where {T,N} = + OffsetArray{T,N}(uninitialized, inds) + +Base.IndexStyle(::Type{T}) where {T<:OffsetArray} = Base.IndexStyle(parenttype(T)) +parenttype(::Type{OffsetArray{T,N,AA}}) where {T,N,AA} = AA +parenttype(A::OffsetArray) = parenttype(typeof(A)) + +Base.parent(A::OffsetArray) = A.parent + +errmsg(A) = error("size not supported for arrays with indices $(axes(A)); see https://docs.julialang.org/en/latest/devdocs/offset-arrays/") +Base.size(A::OffsetArray) = errmsg(A) +Base.size(A::OffsetArray, d) = errmsg(A) +Base.eachindex(::IndexCartesian, A::OffsetArray) = CartesianIndices(axes(A)) +Base.eachindex(::IndexLinear, A::OffsetVector) = axes(A, 1) + +# Implementations of indices and indices1. Since bounds-checking is +# performance-critical and relies on indices, these are usually worth +# optimizing thoroughly. +@inline Base.axes(A::OffsetArray, d) = 1 <= d <= length(A.offsets) ? axes(parent(A))[d] .+ A.offsets[d] : (1:1) +@inline Base.axes(A::OffsetArray) = _indices(axes(parent(A)), A.offsets) # would rather use ntuple, but see #15276 +@inline _indices(inds, offsets) = (inds[1] .+ offsets[1], _indices(tail(inds), tail(offsets))...) +_indices(::Tuple{}, ::Tuple{}) = () +Base.indices1(A::OffsetArray{T,0}) where {T} = 1:1 # we only need to specialize this one + +function Base.similar(A::OffsetArray, T::Type, dims::Dims) + B = similar(parent(A), T, dims) +end +function Base.similar(A::AbstractArray, T::Type, inds::Tuple{UnitRange,Vararg{UnitRange}}) + B = similar(A, T, map(length, inds)) + OffsetArray(B, map(indsoffset, inds)) +end + +Base.similar(f::Union{Function,Type}, shape::Tuple{UnitRange,Vararg{UnitRange}}) = + OffsetArray(f(map(length, shape)), map(indsoffset, shape)) +Base.similar(::Type{T}, shape::Tuple{UnitRange,Vararg{UnitRange}}) where {T<:Array} = + OffsetArray(T(uninitialized, map(length, shape)), map(indsoffset, shape)) +Base.similar(::Type{T}, shape::Tuple{UnitRange,Vararg{UnitRange}}) where {T<:BitArray} = + OffsetArray(T(uninitialized, map(length, shape)), map(indsoffset, shape)) + +Base.reshape(A::AbstractArray, inds::Tuple{UnitRange,Vararg{UnitRange}}) = OffsetArray(reshape(A, map(length, inds)), map(indsoffset, inds)) + +@inline function Base.getindex(A::OffsetArray{T,N}, I::Vararg{Int,N}) where {T,N} + checkbounds(A, I...) + @inbounds ret = parent(A)[offset(A.offsets, I)...] + ret +end +# Vectors don't support one-based linear indexing; they always use the offsets +@inline function Base.getindex(A::OffsetVector, i::Int) + checkbounds(A, i) + @inbounds ret = parent(A)[offset(A.offsets, (i,))[1]] + ret +end +# But multidimensional arrays allow one-based linear indexing +@inline function Base.getindex(A::OffsetArray, i::Int) + checkbounds(A, i) + @inbounds ret = parent(A)[i] + ret +end +@inline function Base.setindex!(A::OffsetArray{T,N}, val, I::Vararg{Int,N}) where {T,N} + checkbounds(A, I...) + @inbounds parent(A)[offset(A.offsets, I)...] = val + val +end +@inline function Base.setindex!(A::OffsetVector, val, i::Int) + checkbounds(A, i) + @inbounds parent(A)[offset(A.offsets, (i,))[1]] = val + val +end +@inline function Base.setindex!(A::OffsetArray, val, i::Int) + checkbounds(A, i) + @inbounds parent(A)[i] = val + val +end + +@inline function Base.deleteat!(A::OffsetArray, i::Int) + checkbounds(A, i) + @inbounds deleteat!(parent(A), offset(A.offsets, (i,))[1]) +end + +@inline function Base.deleteat!(A::OffsetArray{T,N}, I::Vararg{Int, N}) where {T,N} + checkbounds(A, I...) + @inbounds deleteat!(parent(A), offset(A.offsets, I)...) +end + +@inline function Base.deleteat!(A::OffsetArray, i::UnitRange{Int}) + checkbounds(A, first(i)) + checkbounds(A, last(i)) + first_idx = offset(A.offsets, (first(i),))[1] + last_idx = offset(A.offsets, (last(i),))[1] + @inbounds deleteat!(parent(A), first_idx:last_idx) +end + +# Computing a shifted index (subtracting the offset) +offset(offsets::NTuple{N,Int}, inds::NTuple{N,Int}) where {N} = _offset((), offsets, inds) +_offset(out, ::Tuple{}, ::Tuple{}) = out +@inline _offset(out, offsets, inds) = _offset((out..., inds[1]-offsets[1]), Base.tail(offsets), Base.tail(inds)) + +indsoffset(r::AbstractRange) = first(r) - 1 +indsoffset(i::Integer) = 0 + +Base.resize!(A::OffsetVector, nl::Integer) = (resize!(A.parent, nl); A) + +end diff --git a/test/random.jl b/stdlib/Random/test/runtests.jl similarity index 95% rename from test/random.jl rename to stdlib/Random/test/runtests.jl index 19d22b4d6edc2..79856927a8ef3 100644 --- a/test/random.jl +++ b/stdlib/Random/test/runtests.jl @@ -1,11 +1,14 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -isdefined(Main, :TestHelpers) || @eval Main include(joinpath(dirname(@__FILE__), "TestHelpers.jl")) -using Main.TestHelpers.OAs -using SparseArrays +using Test, SparseArrays -using Base.Random.dSFMT -using Base.Random: Sampler, SamplerRangeFast, SamplerRangeInt, MT_CACHE_F, MT_CACHE_I +include("OAs.jl") +using .OAs + +using Random +using Random.dSFMT + +using Random: Sampler, SamplerRangeFast, SamplerRangeInt, MT_CACHE_F, MT_CACHE_I @testset "Issue #6573" begin srand(0) @@ -94,10 +97,10 @@ for T in (Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt end end -@test !any([(Base.Random.maxmultiple(i)+i) > 0xFF for i in 0x00:0xFF]) -@test all([(Base.Random.maxmultiple(i)+1) % i for i in 0x01:0xFF] .== 0) -@test all([(Base.Random.maxmultiple(i)+1+i) > 0xFF for i in 0x00:0xFF]) -@test length(0x00:0xFF)== Base.Random.maxmultiple(0x0)+1 +@test !any([(Random.maxmultiple(i)+i) > 0xFF for i in 0x00:0xFF]) +@test all([(Random.maxmultiple(i)+1) % i for i in 0x01:0xFF] .== 0) +@test all([(Random.maxmultiple(i)+1+i) > 0xFF for i in 0x00:0xFF]) +@test length(0x00:0xFF)== Random.maxmultiple(0x0)+1 if sizeof(Int32) < sizeof(Int) @@ -110,7 +113,6 @@ if sizeof(Int32) < sizeof(Int) @test all(div(one(UInt128) << 64, k)*k - 1 == SamplerRangeInt(map(U, 1:k)).u for k in 13 .+ Int64(2).^(52:62)) end - end # BigInt specific @@ -207,12 +209,12 @@ function randmtzig_fill_ziggurat_tables() # Operates on the global arrays return nothing end randmtzig_fill_ziggurat_tables() -@test all(ki == Base.Random.ki) -@test all(wi == Base.Random.wi) -@test all(fi == Base.Random.fi) -@test all(ke == Base.Random.ke) -@test all(we == Base.Random.we) -@test all(fe == Base.Random.fe) +@test all(ki == Random.ki) +@test all(wi == Random.wi) +@test all(fi == Random.fi) +@test all(ke == Random.ke) +@test all(we == Random.we) +@test all(fe == Random.fe) #same random numbers on for small ranges on all systems guardsrand() do @@ -233,7 +235,7 @@ for U in (Int64, UInt64) end -import Base.Random: uuid1, uuid4, UUID, uuid_version +import Random: uuid1, uuid4, UUID, uuid_version # UUID u1 = uuid1() @@ -287,12 +289,12 @@ let mt = MersenneTwister(0) end srand(mt, 0) - AF64 = Vector{Float64}(uninitialized, Base.Random.dsfmt_get_min_array_size()-1) + AF64 = Vector{Float64}(uninitialized, Random.dsfmt_get_min_array_size()-1) @test rand!(mt, AF64)[end] == 0.957735065345398 @test rand!(mt, AF64)[end] == 0.6492481059865669 resize!(AF64, 2*length(mt.vals)) - @test invoke(rand!, Tuple{MersenneTwister,AbstractArray{Float64},Base.Random.SamplerTrivial{Base.Random.CloseOpen01_64}}, - mt, AF64, Base.Random.SamplerTrivial(Base.Random.CloseOpen01()))[end] == 0.1142787906708973 + @test invoke(rand!, Tuple{MersenneTwister,AbstractArray{Float64},Random.SamplerTrivial{Random.CloseOpen01_64}}, + mt, AF64, Random.SamplerTrivial(Random.CloseOpen01()))[end] == 0.1142787906708973 end # Issue #9037 @@ -561,18 +563,18 @@ let seed = rand(UInt32, 10) end # MersenneTwister initialization with invalid values -@test_throws DomainError Base.dSFMT.DSFMT_state(zeros(Int32, rand(0:Base.dSFMT.JN32-1))) +@test_throws DomainError dSFMT.DSFMT_state(zeros(Int32, rand(0:dSFMT.JN32-1))) -@test_throws DomainError MersenneTwister(zeros(UInt32, 1), Base.dSFMT.DSFMT_state(), +@test_throws DomainError MersenneTwister(zeros(UInt32, 1), dSFMT.DSFMT_state(), zeros(Float64, 10), zeros(UInt128, MT_CACHE_I>>4), 0, 0) -@test_throws DomainError MersenneTwister(zeros(UInt32, 1), Base.dSFMT.DSFMT_state(), +@test_throws DomainError MersenneTwister(zeros(UInt32, 1), dSFMT.DSFMT_state(), zeros(Float64, MT_CACHE_F), zeros(UInt128, MT_CACHE_I>>4), -1, 0) -@test_throws DomainError MersenneTwister(zeros(UInt32, 1), Base.dSFMT.DSFMT_state(), +@test_throws DomainError MersenneTwister(zeros(UInt32, 1), dSFMT.DSFMT_state(), zeros(Float64, MT_CACHE_F), zeros(UInt128, MT_CACHE_I>>3), 0, 0) -@test_throws DomainError MersenneTwister(zeros(UInt32, 1), Base.dSFMT.DSFMT_state(), +@test_throws DomainError MersenneTwister(zeros(UInt32, 1), dSFMT.DSFMT_state(), zeros(Float64, MT_CACHE_F), zeros(UInt128, MT_CACHE_I>>4), 0, -1) # seed is private to MersenneTwister @@ -591,7 +593,7 @@ end # srand(rng, ...) returns rng (#21248) guardsrand() do - g = Base.Random.GLOBAL_RNG + g = Random.GLOBAL_RNG m = MersenneTwister(0) @test srand() === g @test srand(rand(UInt)) === g @@ -604,8 +606,8 @@ end # Issue 20062 - ensure internal functions reserve_1, reserve are type-stable let r = MersenneTwister(0) - @inferred Base.Random.reserve_1(r) - @inferred Base.Random.reserve(r, 1) + @inferred Random.reserve_1(r) + @inferred Random.reserve(r, 1) end # test randstring API diff --git a/stdlib/SharedArrays/src/SharedArrays.jl b/stdlib/SharedArrays/src/SharedArrays.jl index d7b933c723087..d66c2c42b956e 100644 --- a/stdlib/SharedArrays/src/SharedArrays.jl +++ b/stdlib/SharedArrays/src/SharedArrays.jl @@ -7,11 +7,11 @@ Provide the [`SharedArray`](@ref) type. It represents an array, which is shared """ module SharedArrays -using Mmap, Distributed +using Mmap, Distributed, Random import Base: length, size, ndims, IndexStyle, reshape, convert, deepcopy_internal, serialize, deserialize, - show, getindex, setindex!, fill!, rand!, similar, reduce, map!, copyto!, unsafe_convert -import Base.Random + show, getindex, setindex!, fill!, similar, reduce, map!, copyto!, unsafe_convert +import Random import Base.Serializer: serialize_cycle_header, serialize_type, writetag, UNDEFREF_TAG import Distributed: RRID, procs import Base.Filesystem: JL_O_CREAT, JL_O_RDWR, S_IRUSR, S_IWUSR @@ -507,7 +507,7 @@ function fill!(S::SharedArray, v) return S end -function rand!(S::SharedArray{T}) where T +function Random.rand!(S::SharedArray{T}) where T f = S->map!(x -> rand(T), S.loc_subarr_1d, S.loc_subarr_1d) @sync for p in procs(S) @async remotecall_wait(f, p, S) diff --git a/stdlib/SharedArrays/test/runtests.jl b/stdlib/SharedArrays/test/runtests.jl index c8d075059a99e..f0ce9ad960114 100644 --- a/stdlib/SharedArrays/test/runtests.jl +++ b/stdlib/SharedArrays/test/runtests.jl @@ -1,6 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -using Test, Distributed, SharedArrays +using Test, Distributed, SharedArrays, Random include(joinpath(Sys.BINDIR, "..", "share", "julia", "test", "testenv.jl")) addprocs_with_testenv(4) diff --git a/stdlib/SparseArrays/src/SparseArrays.jl b/stdlib/SparseArrays/src/SparseArrays.jl index ab00566065773..6b5a3c8ed1835 100644 --- a/stdlib/SparseArrays/src/SparseArrays.jl +++ b/stdlib/SparseArrays/src/SparseArrays.jl @@ -28,6 +28,8 @@ import Base: @get!, acos, acosd, acot, acotd, acsch, asech, asin, asind, asinh, rotl90, rotr90, round, scale!, setindex!, similar, size, transpose, tril, triu, vec, permute!, map, map!, Array +using Random: defaultRNG, AbstractRNG, randsubseq, randsubseq! + export AbstractSparseArray, AbstractSparseMatrix, AbstractSparseVector, SparseMatrixCSC, SparseVector, blkdiag, droptol!, dropzeros!, dropzeros, issparse, nonzeros, nzrange, rowvals, sparse, sparsevec, spdiagm, diff --git a/stdlib/SparseArrays/src/sparsematrix.jl b/stdlib/SparseArrays/src/sparsematrix.jl index edbdd41eb52ba..8311662e81fbe 100644 --- a/stdlib/SparseArrays/src/sparsematrix.jl +++ b/stdlib/SparseArrays/src/sparsematrix.jl @@ -1349,7 +1349,6 @@ function _sparse_findprevnz(m::SparseMatrixCSC, i::Integer) return LinearIndices(m)[m.rowval[prevhi-1], prevcol] end -import Base.Random.GLOBAL_RNG function sprand_IJ(r::AbstractRNG, m::Integer, n::Integer, density::AbstractFloat) ((m < 0) || (n < 0)) && throw(ArgumentError("invalid Array dimensions")) 0 <= density <= 1 || throw(ArgumentError("$density not in [0,1]")) @@ -1427,18 +1426,18 @@ function sprand(m::Integer, n::Integer, density::AbstractFloat, N == 0 && return spzeros(T,m,n) N == 1 && return rand() <= density ? sparse([1], [1], rfn(1)) : spzeros(T,1,1) - I,J = sprand_IJ(GLOBAL_RNG, m, n, density) + I,J = sprand_IJ(defaultRNG(), m, n, density) sparse_IJ_sorted!(I, J, rfn(length(I)), m, n, +) # it will never need to combine end truebools(r::AbstractRNG, n::Integer) = fill(true, n) -sprand(m::Integer, n::Integer, density::AbstractFloat) = sprand(GLOBAL_RNG,m,n,density) +sprand(m::Integer, n::Integer, density::AbstractFloat) = sprand(defaultRNG(),m,n,density) sprand(r::AbstractRNG, m::Integer, n::Integer, density::AbstractFloat) = sprand(r,m,n,density,rand,Float64) sprand(r::AbstractRNG, ::Type{T}, m::Integer, n::Integer, density::AbstractFloat) where {T} = sprand(r,m,n,density,(r, i) -> rand(r, T, i), T) sprand(r::AbstractRNG, ::Type{Bool}, m::Integer, n::Integer, density::AbstractFloat) = sprand(r,m,n,density, truebools, Bool) -sprand(::Type{T}, m::Integer, n::Integer, density::AbstractFloat) where {T} = sprand(GLOBAL_RNG, T, m, n, density) +sprand(::Type{T}, m::Integer, n::Integer, density::AbstractFloat) where {T} = sprand(defaultRNG(), T, m, n, density) """ sprandn([rng], m[,n],p::AbstractFloat) @@ -1460,7 +1459,7 @@ julia> sprandn(rng, 2, 2, 0.75) ``` """ sprandn(r::AbstractRNG, m::Integer, n::Integer, density::AbstractFloat) = sprand(r,m,n,density,randn,Float64) -sprandn(m::Integer, n::Integer, density::AbstractFloat) = sprandn(GLOBAL_RNG,m,n,density) +sprandn(m::Integer, n::Integer, density::AbstractFloat) = sprandn(defaultRNG(),m,n,density) LinAlg.fillstored!(S::SparseMatrixCSC, x) = (fill!(view(S.nzval, 1:(S.colptr[S.n + 1] - 1)), x); S) diff --git a/stdlib/SparseArrays/src/sparsevector.jl b/stdlib/SparseArrays/src/sparsevector.jl index d86e6ec763453..dd9c979bd349b 100644 --- a/stdlib/SparseArrays/src/sparsevector.jl +++ b/stdlib/SparseArrays/src/sparsevector.jl @@ -472,28 +472,28 @@ copyto!(A::SparseMatrixCSC, B::SparseVector{TvB,TiB}) where {TvB,TiB} = ### Rand Construction -sprand(n::Integer, p::AbstractFloat, rfn::Function, ::Type{T}) where {T} = sprand(GLOBAL_RNG, n, p, rfn, T) +sprand(n::Integer, p::AbstractFloat, rfn::Function, ::Type{T}) where {T} = sprand(defaultRNG(), n, p, rfn, T) function sprand(r::AbstractRNG, n::Integer, p::AbstractFloat, rfn::Function, ::Type{T}) where T I = randsubseq(r, 1:convert(Int, n), p) V = rfn(r, T, length(I)) SparseVector(n, I, V) end -sprand(n::Integer, p::AbstractFloat, rfn::Function) = sprand(GLOBAL_RNG, n, p, rfn) +sprand(n::Integer, p::AbstractFloat, rfn::Function) = sprand(defaultRNG(), n, p, rfn) function sprand(r::AbstractRNG, n::Integer, p::AbstractFloat, rfn::Function) I = randsubseq(r, 1:convert(Int, n), p) V = rfn(r, length(I)) SparseVector(n, I, V) end -sprand(n::Integer, p::AbstractFloat) = sprand(GLOBAL_RNG, n, p, rand) +sprand(n::Integer, p::AbstractFloat) = sprand(defaultRNG(), n, p, rand) sprand(r::AbstractRNG, n::Integer, p::AbstractFloat) = sprand(r, n, p, rand) sprand(r::AbstractRNG, ::Type{T}, n::Integer, p::AbstractFloat) where {T} = sprand(r, n, p, (r, i) -> rand(r, T, i)) sprand(r::AbstractRNG, ::Type{Bool}, n::Integer, p::AbstractFloat) = sprand(r, n, p, truebools) -sprand(::Type{T}, n::Integer, p::AbstractFloat) where {T} = sprand(GLOBAL_RNG, T, n, p) +sprand(::Type{T}, n::Integer, p::AbstractFloat) where {T} = sprand(defaultRNG(), T, n, p) -sprandn(n::Integer, p::AbstractFloat) = sprand(GLOBAL_RNG, n, p, randn) +sprandn(n::Integer, p::AbstractFloat) = sprand(defaultRNG(), n, p, randn) sprandn(r::AbstractRNG, n::Integer, p::AbstractFloat) = sprand(r, n, p, randn) ## Indexing into Matrices can return SparseVectors diff --git a/stdlib/SparseArrays/test/higherorderfns.jl b/stdlib/SparseArrays/test/higherorderfns.jl index 9fd045a9d5f2a..1e0516102cf00 100644 --- a/stdlib/SparseArrays/test/higherorderfns.jl +++ b/stdlib/SparseArrays/test/higherorderfns.jl @@ -4,6 +4,8 @@ # base/sparse/higherorderfns.jl, particularly map[!]/broadcast[!] for SparseVectors and # SparseMatrixCSCs at present. +using Random + @testset "map[!] implementation specialized for a single (input) sparse vector/matrix" begin N, M = 10, 12 for shapeA in ((N,), (N, M)) diff --git a/stdlib/SparseArrays/test/sparse.jl b/stdlib/SparseArrays/test/sparse.jl index f9574ce5396ca..ee7400b816c13 100644 --- a/stdlib/SparseArrays/test/sparse.jl +++ b/stdlib/SparseArrays/test/sparse.jl @@ -2,6 +2,7 @@ using Base.LinAlg: mul!, ldiv!, rdiv! using Base.Printf: @printf +using Random @testset "issparse" begin @test issparse(sparse(fill(1,5,5))) diff --git a/stdlib/SparseArrays/test/sparsevector.jl b/stdlib/SparseArrays/test/sparsevector.jl index 9112feaa6a2b5..199f49ab6d963 100644 --- a/stdlib/SparseArrays/test/sparsevector.jl +++ b/stdlib/SparseArrays/test/sparsevector.jl @@ -1,6 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license using Base.LinAlg: mul!, ldiv! +using Random ### Data diff --git a/stdlib/SuiteSparse/test/runtests.jl b/stdlib/SuiteSparse/test/runtests.jl index 4cf807452f474..861dbb4a00246 100644 --- a/stdlib/SuiteSparse/test/runtests.jl +++ b/stdlib/SuiteSparse/test/runtests.jl @@ -1,6 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -using Test +using Test, Random using SuiteSparse, SparseArrays if Base.USE_GPL_LIBS diff --git a/stdlib/Test/src/Test.jl b/stdlib/Test/src/Test.jl index ced6a953fc7a2..6cbdb195d037a 100644 --- a/stdlib/Test/src/Test.jl +++ b/stdlib/Test/src/Test.jl @@ -18,6 +18,7 @@ module Test export @test, @test_throws, @test_broken, @test_skip, @test_warn, @test_nowarn, @test_logs, @test_deprecated + export @testset # Legacy approximate testing functions, yet to be included export @inferred @@ -27,6 +28,8 @@ export guardsrand, TestSetException import Distributed: myid +using Random: srand, AbstractRNG, GLOBAL_RNG + #----------------------------------------------------------------------- # Backtrace utility functions @@ -997,17 +1000,17 @@ function testset_beginend(args, tests, source) # we reproduce the logic of guardsrand, but this function # cannot be used as it changes slightly the semantic of @testset, # by wrapping the body in a function - oldrng = copy(Base.GLOBAL_RNG) + oldrng = copy(GLOBAL_RNG) try # GLOBAL_RNG is re-seeded with its own seed to ease reproduce a failed test - srand(Base.GLOBAL_RNG.seed) + srand(GLOBAL_RNG.seed) $(esc(tests)) catch err # something in the test block threw an error. Count that as an # error in this test set record(ts, Error(:nontest_error, :(), err, catch_backtrace(), $(QuoteNode(source)))) finally - copy!(Base.GLOBAL_RNG, oldrng) + copy!(GLOBAL_RNG, oldrng) end pop_testset() finish(ts) @@ -1066,7 +1069,7 @@ function testset_forloop(args, testloop, source) pop_testset() push!(arr, finish(ts)) # it's 1000 times faster to copy from tmprng rather than calling srand - copy!(Base.GLOBAL_RNG, tmprng) + copy!(GLOBAL_RNG, tmprng) end ts = $(testsettype)($desc; $options...) @@ -1084,9 +1087,9 @@ function testset_forloop(args, testloop, source) arr = Vector{Any}() local first_iteration = true local ts - local oldrng = copy(Base.GLOBAL_RNG) - srand(Base.GLOBAL_RNG.seed) - local tmprng = copy(Base.GLOBAL_RNG) + local oldrng = copy(GLOBAL_RNG) + srand(GLOBAL_RNG.seed) + local tmprng = copy(GLOBAL_RNG) try $(Expr(:for, Expr(:block, [esc(v) for v in loopvars]...), blk)) finally @@ -1095,7 +1098,7 @@ function testset_forloop(args, testloop, source) pop_testset() push!(arr, finish(ts)) end - copy!(Base.GLOBAL_RNG, oldrng) + copy!(GLOBAL_RNG, oldrng) end arr end @@ -1506,7 +1509,7 @@ Base.similar(A::GenericArray, s::Integer...) = GenericArray(similar(A.a, s...)) "`guardsrand(f)` runs the function `f()` and then restores the state of the global RNG as it was before." -function guardsrand(f::Function, r::AbstractRNG=Base.GLOBAL_RNG) +function guardsrand(f::Function, r::AbstractRNG=GLOBAL_RNG) old = copy(r) try f() diff --git a/stdlib/Test/test/runtests.jl b/stdlib/Test/test/runtests.jl index 12eb63e0386c3..56ba8e8a15fd7 100644 --- a/stdlib/Test/test/runtests.jl +++ b/stdlib/Test/test/runtests.jl @@ -1,6 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -using Test, Distributed +using Test, Distributed, Random import Logging: Debug, Info, Warn @@ -614,7 +614,7 @@ end @testset "test guarded srand" begin seed = rand(UInt) - orig = copy(Base.GLOBAL_RNG) + 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) @@ -628,7 +628,7 @@ end end::Tuple{Float64,Int} @test a == c == rand(r1) == rand(r2) @test b == d == rand(r1, Int) == rand(r2, Int) - @test orig == Base.GLOBAL_RNG + @test orig == Random.GLOBAL_RNG @test rand(orig) == rand() end diff --git a/test/abstractarray.jl b/test/abstractarray.jl index ccbbfd3f67b79..64d23dc4652fd 100644 --- a/test/abstractarray.jl +++ b/test/abstractarray.jl @@ -1,6 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -using SparseArrays +using Random, SparseArrays A = rand(5,4,3) @testset "Bounds checking" begin diff --git a/test/arrayops.jl b/test/arrayops.jl index cdb8fa2d67645..2cf815ed5fb0f 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -5,6 +5,8 @@ isdefined(Main, :TestHelpers) || @eval Main include("TestHelpers.jl") using Main.TestHelpers.OAs using SparseArrays +using Random + @testset "basics" begin @test length([1, 2, 3]) == 3 @test count(!iszero, [1, 2, 3]) == 3 diff --git a/test/asyncmap.jl b/test/asyncmap.jl index 0908cdea4890f..1a23290679f2b 100644 --- a/test/asyncmap.jl +++ b/test/asyncmap.jl @@ -1,5 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license +using Random + # Test asyncmap @test allunique(asyncmap(x->(sleep(1.0);object_id(current_task())), 1:10)) diff --git a/test/bigint.jl b/test/bigint.jl index dddcc66c467fd..0c29a8a961aa9 100644 --- a/test/bigint.jl +++ b/test/bigint.jl @@ -1,5 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license +using Random + a = parse(BigInt,"123456789012345678901234567890") b = parse(BigInt,"123456789012345678901234567891") c = parse(BigInt,"246913578024691357802469135780") diff --git a/test/bitarray.jl b/test/bitarray.jl index 27997587c35d0..99afbbfe41498 100644 --- a/test/bitarray.jl +++ b/test/bitarray.jl @@ -1,6 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license using Base: findprevnot, findnextnot +using Random tc(r1::NTuple{N,Any}, r2::NTuple{N,Any}) where {N} = all(x->tc(x...), [zip(r1,r2)...]) tc(r1::BitArray{N}, r2::Union{BitArray{N},Array{Bool,N}}) where {N} = true diff --git a/test/bitset.jl b/test/bitset.jl index c6ab52adc52c1..b62ea9bacc1c8 100644 --- a/test/bitset.jl +++ b/test/bitset.jl @@ -2,6 +2,8 @@ # Test functionality of BitSet +using Random + @testset "Construction, collect" begin data_in = (1,5,100) s = BitSet(data_in) diff --git a/test/boundscheck_exec.jl b/test/boundscheck_exec.jl index ee8413bd4e5b9..138059d8fcb8a 100644 --- a/test/boundscheck_exec.jl +++ b/test/boundscheck_exec.jl @@ -2,7 +2,7 @@ module TestBoundsCheck -using Test +using Test, Random @enum BCOption bc_default bc_on bc_off bc_opt = BCOption(Base.JLOptions().check_bounds) diff --git a/test/broadcast.jl b/test/broadcast.jl index e8c17fda643b7..df176eacde2c1 100644 --- a/test/broadcast.jl +++ b/test/broadcast.jl @@ -4,7 +4,7 @@ module TestBroadcastInternals using Base.Broadcast: check_broadcast_indices, check_broadcast_shape, newindex, _bcs using Base: OneTo -using Test +using Test, Random @test @inferred(_bcs((3,5), (3,5))) == (3,5) @test @inferred(_bcs((3,1), (3,5))) == (3,5) diff --git a/test/ccall.jl b/test/ccall.jl index 855fb0f30eb37..4250591b2ac4f 100644 --- a/test/ccall.jl +++ b/test/ccall.jl @@ -1,6 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license import Base.copy, Base.== +using Random import Libdl diff --git a/test/channels.jl b/test/channels.jl index 8fe69c10f1c64..10301bd3e804a 100644 --- a/test/channels.jl +++ b/test/channels.jl @@ -1,5 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license +using Random + # Test various constructors let c = Channel(1) @test eltype(c) == Any diff --git a/test/choosetests.jl b/test/choosetests.jl index fd8731b2937d7..2c9ec6a42d640 100644 --- a/test/choosetests.jl +++ b/test/choosetests.jl @@ -1,5 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license +using Random + const STDLIB_DIR = joinpath(Sys.BINDIR, "..", "share", "julia", "site", "v$(VERSION.major).$(VERSION.minor)") const STDLIBS = readdir(STDLIB_DIR) @@ -35,7 +37,7 @@ function choosetests(choices = []) "keywordargs", "numbers", "subtype", "char", "strings", "triplequote", "unicode", "intrinsics", "dict", "hashing", "iobuffer", "staged", "offsetarray", - "arrayops", "tuple", "reduce", "reducedim", "random", "abstractarray", + "arrayops", "tuple", "reduce", "reducedim", "abstractarray", "intfuncs", "simdloop", "vecelement", "bitarray", "copy", "math", "fastmath", "functional", "iterators", "operators", "path", "ccall", "parse", "loading", "bigint", diff --git a/test/codegen.jl b/test/codegen.jl index 3d3be9ba537fd..0f5909b32d665 100644 --- a/test/codegen.jl +++ b/test/codegen.jl @@ -2,6 +2,8 @@ # tests for codegen and optimizations +using Random + const opt_level = Base.JLOptions().opt_level const coverage = (Base.JLOptions().code_coverage > 0) || (Base.JLOptions().malloc_log > 0) const Iptr = sizeof(Int) == 8 ? "i64" : "i32" diff --git a/test/combinatorics.jl b/test/combinatorics.jl index 307f7c0c7fcf9..5d1aa0af564f5 100644 --- a/test/combinatorics.jl +++ b/test/combinatorics.jl @@ -1,5 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license +using Random: randcycle + @testset "binomial" begin @test binomial(5,-1) == 0 @test binomial(5,10) == 0 diff --git a/test/compile.jl b/test/compile.jl index 2da53881b67b4..392f7988d0bd0 100644 --- a/test/compile.jl +++ b/test/compile.jl @@ -1,6 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -using Test, Distributed +using Test, Distributed, Random import Base: root_module @@ -218,9 +218,9 @@ try [:Base, :Core, Foo2_module, FooBase_module, :Main]), # plus modules included in the system image Dict(s => Base.module_uuid(Base.root_module(s)) for s in - [:Base64, :CRC32c, :Dates, :DelimitedFiles, :FileWatching, :Future, - :IterativeEigensolvers, :Logging, :Mmap, :Printf, :Profile, :SharedArrays, - :SparseArrays, :SuiteSparse, :Test, :Unicode, :Distributed, :Libdl])) + [:Base64, :CRC32c, :Dates, :DelimitedFiles, :Distributed, :FileWatching, + :Future, :IterativeEigensolvers, :Libdl, :Logging, :Mmap, :Printf, + :Profile, :Random, :SharedArrays, :SparseArrays, :SuiteSparse, :Test, :Unicode])) @test discard_module.(deps) == deps1 @test current_task()(0x01, 0x4000, 0x30031234) == 2 diff --git a/test/copy.jl b/test/copy.jl index d4c8ba2f2ea42..80052a1cc6465 100644 --- a/test/copy.jl +++ b/test/copy.jl @@ -1,5 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license +using Random + mainres = ([4, 5, 3], [1, 5, 3]) bitres = ([true, true, false], diff --git a/test/core.jl b/test/core.jl index eeb59026e9563..61218c91c3f29 100644 --- a/test/core.jl +++ b/test/core.jl @@ -1,6 +1,9 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license # test core language features + +using Random + const Bottom = Union{} using SparseArrays @@ -4192,7 +4195,7 @@ end # issue #14113 module A14113 - using Test + using Test, Random # show that making several thousand methods (and lots of AST constants) # doesn't cause any serious issues (for example, for the serializer) # although to keep runtime on the order of several seconds for this test, @@ -5536,6 +5539,7 @@ module UnionOptimizations using Test using Dates +using Random const boxedunions = [Union{}, Union{String, Nothing}] const unboxedunions = [Union{Int8, Nothing}, diff --git a/test/dict.jl b/test/dict.jl index 464f5f342ac7e..8b3cd568c1dd6 100644 --- a/test/dict.jl +++ b/test/dict.jl @@ -1,5 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license +using Random + @testset "Pair" begin p = Pair(10,20) @test p == (10=>20) diff --git a/test/env.jl b/test/env.jl index ea24759aa5941..2ea6eedafd67c 100644 --- a/test/env.jl +++ b/test/env.jl @@ -1,5 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license +using Random + @test !("f=a=k=e=n=a=m=e" ∈ keys(ENV)) @testset "issue #10994" begin diff --git a/test/examples.jl b/test/examples.jl index 378e9038deda2..a695dfd585433 100644 --- a/test/examples.jl +++ b/test/examples.jl @@ -1,5 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license +using Random + dir = joinpath(Sys.BINDIR, Base.DOCDIR, "examples") include(joinpath(dir, "bubblesort.jl")) diff --git a/test/floatfuncs.jl b/test/floatfuncs.jl index f5f0f7472935a..b686c45b4186c 100644 --- a/test/floatfuncs.jl +++ b/test/floatfuncs.jl @@ -1,6 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -using Test +using Test, Random # test the basic floating point functions diff --git a/test/hashing.jl b/test/hashing.jl index bcc4c33976ac9..4de1b7d988631 100644 --- a/test/hashing.jl +++ b/test/hashing.jl @@ -1,6 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -using SparseArrays +using Random, SparseArrays types = Any[ Bool, diff --git a/test/inference.jl b/test/inference.jl index d407db81a77ee..ffdede50d5b90 100644 --- a/test/inference.jl +++ b/test/inference.jl @@ -4,6 +4,8 @@ import Core.Inference: Const, Conditional, ⊑ const isleaftype = Core.Inference._isleaftype +using Random + # demonstrate some of the type-size limits @test Core.Inference.limit_type_size(Ref{Complex{T} where T}, Ref, Ref, 0) == Ref @test Core.Inference.limit_type_size(Ref{Complex{T} where T}, Ref{Complex{T} where T}, Ref, 0) == Ref{Complex{T} where T} diff --git a/test/int.jl b/test/int.jl index ba4c998bdc4bb..48d59ccb504d4 100644 --- a/test/int.jl +++ b/test/int.jl @@ -2,6 +2,7 @@ # Test integer conversion routines from int.jl +using Random for y in (-4, Float32(-4), -4.0, big(-4.0)) @test flipsign(3, y) == -3 diff --git a/test/intfuncs.jl b/test/intfuncs.jl index 918febfde41ca..82c67f30e745e 100644 --- a/test/intfuncs.jl +++ b/test/intfuncs.jl @@ -1,5 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license +using Random + @testset "gcd/lcm" begin # Int32 and Int64 take different code paths -- test both for T in (Int32, Int64) diff --git a/test/iobuffer.jl b/test/iobuffer.jl index d0d9a8bf602f1..7b0ad7fb2224d 100644 --- a/test/iobuffer.jl +++ b/test/iobuffer.jl @@ -1,5 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license +using Random + ioslength(io::IOBuffer) = (io.seekable ? io.size : nb_available(io)) bufcontents(io::Base.GenericIOBuffer) = unsafe_string(pointer(io.data), io.size) diff --git a/test/iterators.jl b/test/iterators.jl index c312f76c8d3be..ed8f046240739 100644 --- a/test/iterators.jl +++ b/test/iterators.jl @@ -1,6 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license using Base.Iterators +using Random # zip and filter iterators # issue #4718 diff --git a/test/libgit2-online.jl b/test/libgit2-online.jl index 27dcd76374f85..16f7b18779e42 100644 --- a/test/libgit2-online.jl +++ b/test/libgit2-online.jl @@ -1,5 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license +using Random + ######### # TESTS # ######### diff --git a/test/libgit2.jl b/test/libgit2.jl index aab5785a1ae7d..7d66638add955 100644 --- a/test/libgit2.jl +++ b/test/libgit2.jl @@ -3,6 +3,8 @@ isdefined(Main, :TestHelpers) || @eval Main include(joinpath(@__DIR__, "TestHelpers.jl")) import Main.TestHelpers: challenge_prompt +using Random + const LIBGIT2_MIN_VER = v"0.23.0" const LIBGIT2_HELPER_PATH = joinpath(@__DIR__, "libgit2-helpers.jl") diff --git a/test/linalg/bidiag.jl b/test/linalg/bidiag.jl index b1eedd0b9ff7a..11fa8dfdf5732 100644 --- a/test/linalg/bidiag.jl +++ b/test/linalg/bidiag.jl @@ -1,6 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -using Test +using Test, Random using Base.LinAlg: mul! import Base.LinAlg: BlasReal, BlasFloat using SparseArrays diff --git a/test/linalg/blas.jl b/test/linalg/blas.jl index b829569ed7bcc..02379415ce630 100644 --- a/test/linalg/blas.jl +++ b/test/linalg/blas.jl @@ -1,5 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license +using Random + import Base.LinAlg, Base.LinAlg.BlasReal, Base.LinAlg.BlasComplex srand(100) diff --git a/test/linalg/bunchkaufman.jl b/test/linalg/bunchkaufman.jl index 61bfa395dd277..8189023bd2b4a 100644 --- a/test/linalg/bunchkaufman.jl +++ b/test/linalg/bunchkaufman.jl @@ -1,6 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -using Test +using Test, Random using Base: getproperty using Base.LinAlg: BlasComplex, BlasFloat, BlasReal, QRPivoted diff --git a/test/linalg/cholesky.jl b/test/linalg/cholesky.jl index 620272e6677d8..5db4042b4cc89 100644 --- a/test/linalg/cholesky.jl +++ b/test/linalg/cholesky.jl @@ -1,6 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -using Test +using Test, Random using Base.LinAlg: BlasComplex, BlasFloat, BlasReal, QRPivoted, PosDefException diff --git a/test/linalg/dense.jl b/test/linalg/dense.jl index 84db0500926e8..88427bd0d4912 100644 --- a/test/linalg/dense.jl +++ b/test/linalg/dense.jl @@ -1,6 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -using Test +using Test, Random @testset "Check that non-floats are correctly promoted" begin @test [1 0 0; 0 1 0]\[1,1] ≈ [1;1;0] diff --git a/test/linalg/diagonal.jl b/test/linalg/diagonal.jl index 48eaf2bc9ded7..ecfd6e6e533bd 100644 --- a/test/linalg/diagonal.jl +++ b/test/linalg/diagonal.jl @@ -1,6 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -using Test +using Test, Random using Base.LinAlg: mul!, ldiv!, rdiv! import Base.LinAlg: BlasFloat, BlasComplex, SingularException using SparseArrays diff --git a/test/linalg/eigen.jl b/test/linalg/eigen.jl index 645473127b897..9f41d92cec4be 100644 --- a/test/linalg/eigen.jl +++ b/test/linalg/eigen.jl @@ -1,6 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -using Test +using Test, Random using Base.LinAlg: BlasComplex, BlasFloat, BlasReal, QRPivoted diff --git a/test/linalg/generic.jl b/test/linalg/generic.jl index a6c20f4e858da..91768783cca17 100644 --- a/test/linalg/generic.jl +++ b/test/linalg/generic.jl @@ -1,7 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license import Base: -, *, /, \ -using Test +using Test, Random # A custom Quaternion type with minimal defined interface and methods. # Used to test scale and scale! methods to show non-commutativity. diff --git a/test/linalg/givens.jl b/test/linalg/givens.jl index 2b3a4a077bcc9..bb0b04ca9af17 100644 --- a/test/linalg/givens.jl +++ b/test/linalg/givens.jl @@ -1,6 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -using Test +using Test, Random using Base.LinAlg: mul! # Test givens rotations diff --git a/test/linalg/hessenberg.jl b/test/linalg/hessenberg.jl index 48cfb3de877aa..6e155d46ef1f4 100644 --- a/test/linalg/hessenberg.jl +++ b/test/linalg/hessenberg.jl @@ -1,6 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -using Test +using Test, Random using Base.LinAlg: BlasComplex, BlasFloat, BlasReal, QRPivoted diff --git a/test/linalg/lapack.jl b/test/linalg/lapack.jl index 0da8e423e9f0f..07c956d1a5c12 100644 --- a/test/linalg/lapack.jl +++ b/test/linalg/lapack.jl @@ -1,6 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -using Test +using Test, Random import Base.LinAlg.BlasInt diff --git a/test/linalg/lq.jl b/test/linalg/lq.jl index 6505a00074bad..63e353e7ea407 100644 --- a/test/linalg/lq.jl +++ b/test/linalg/lq.jl @@ -1,6 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -using Test +using Test, Random using Base.LinAlg: BlasComplex, BlasFloat, BlasReal, mul! diff --git a/test/linalg/matmul.jl b/test/linalg/matmul.jl index 21513ced88dd1..b51b436872270 100644 --- a/test/linalg/matmul.jl +++ b/test/linalg/matmul.jl @@ -1,6 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -using Test +using Test, Random using Base.LinAlg: mul! @@ -65,7 +65,8 @@ end # Generic AbstractArrays module MyArray15367 - using Test + using Test, Random + struct MyArray{T,N} <: AbstractArray{T,N} data::Array{T,N} end diff --git a/test/linalg/pinv.jl b/test/linalg/pinv.jl index de2d2ef11c6c8..398e0318171e1 100644 --- a/test/linalg/pinv.jl +++ b/test/linalg/pinv.jl @@ -4,7 +4,7 @@ # Test the pseudo-inverse # -using Test +using Test, Random srand(12345) diff --git a/test/linalg/qr.jl b/test/linalg/qr.jl index 4d76a0861e109..53151902c7988 100644 --- a/test/linalg/qr.jl +++ b/test/linalg/qr.jl @@ -1,6 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -using Test +using Test, Random using Base.LinAlg: BlasComplex, BlasFloat, BlasReal, QRPivoted, mul! diff --git a/test/linalg/schur.jl b/test/linalg/schur.jl index 235071019c02f..ea28b2f8fa74c 100644 --- a/test/linalg/schur.jl +++ b/test/linalg/schur.jl @@ -1,6 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -using Test +using Test, Random using Base.LinAlg: BlasComplex, BlasFloat, BlasReal, QRPivoted diff --git a/test/linalg/special.jl b/test/linalg/special.jl index 85239901e0379..e63384323d0e9 100644 --- a/test/linalg/special.jl +++ b/test/linalg/special.jl @@ -1,6 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -using Test +using Test, Random using SparseArrays using Base.LinAlg: mul! diff --git a/test/linalg/svd.jl b/test/linalg/svd.jl index 8f5207c413336..be88caf62203c 100644 --- a/test/linalg/svd.jl +++ b/test/linalg/svd.jl @@ -1,6 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -using Test +using Test, Random using Base.LinAlg: BlasComplex, BlasFloat, BlasReal, QRPivoted diff --git a/test/linalg/symmetric.jl b/test/linalg/symmetric.jl index 9a59addca3e61..0943d92a0822f 100644 --- a/test/linalg/symmetric.jl +++ b/test/linalg/symmetric.jl @@ -1,6 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -using Test +using Test, Random using SparseArrays srand(101) diff --git a/test/linalg/triangular.jl b/test/linalg/triangular.jl index 91f046d8c4463..66bd4b37b6907 100644 --- a/test/linalg/triangular.jl +++ b/test/linalg/triangular.jl @@ -1,7 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license debug = false -using Test +using Test, Random using Base.LinAlg: BlasFloat, errorbounds, full!, naivesub!, transpose!, UnitUpperTriangular, UnitLowerTriangular, mul!, rdiv! diff --git a/test/linalg/tridiag.jl b/test/linalg/tridiag.jl index 7c09384d2eff3..52dc80b961774 100644 --- a/test/linalg/tridiag.jl +++ b/test/linalg/tridiag.jl @@ -1,6 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -using SparseArrays +using Random, SparseArrays #Test equivalence of eigenvectors/singular vectors taking into account possible phase (sign) differences function test_approx_eq_vecs(a::StridedVecOrMat{S}, b::StridedVecOrMat{T}, error=nothing) where {S<:Real,T<:Real} diff --git a/test/linalg/uniformscaling.jl b/test/linalg/uniformscaling.jl index ed4472dc18b1f..498602fbe307a 100644 --- a/test/linalg/uniformscaling.jl +++ b/test/linalg/uniformscaling.jl @@ -1,6 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -using Test +using Test, Random using SparseArrays srand(123) diff --git a/test/math.jl b/test/math.jl index 43040f8c1fb5a..8571b6299917e 100644 --- a/test/math.jl +++ b/test/math.jl @@ -1,5 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license +using Random + function isnan_type(::Type{T}, x) where T isa(x, T) && isnan(x) end diff --git a/test/misc.jl b/test/misc.jl index e5c3fcd51da2c..c97e051c3470d 100644 --- a/test/misc.jl +++ b/test/misc.jl @@ -226,6 +226,7 @@ end # Issue 14173 module Tmp14173 + using Random export A A = randn(2000, 2000) end diff --git a/test/numbers.jl b/test/numbers.jl index dd9adc60ebae4..570ffa5813d59 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -1,6 +1,8 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license using Base.MathConstants +using Random + const ≣ = isequal # convenient for comparing NaNs # remove these tests and re-enable the same ones in the diff --git a/test/offsetarray.jl b/test/offsetarray.jl index 426f01bd4e2d1..cd1c9949c61d1 100644 --- a/test/offsetarray.jl +++ b/test/offsetarray.jl @@ -3,6 +3,7 @@ isdefined(Main, :TestHelpers) || @eval Main include(joinpath(dirname(@__FILE__), "TestHelpers.jl")) using Main.TestHelpers.OAs using DelimitedFiles +using Random const OAs_name = join(fullname(OAs), ".") diff --git a/test/operators.jl b/test/operators.jl index d766241a20692..51a3094f025c9 100644 --- a/test/operators.jl +++ b/test/operators.jl @@ -1,5 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license +using Random: randstring + @test ifelse(true, 1, 2) == 1 @test ifelse(false, 1, 2) == 2 diff --git a/test/perf/perfutil.jl b/test/perf/perfutil.jl index 417c6e640ff5b..7c923aa3612a0 100644 --- a/test/perf/perfutil.jl +++ b/test/perf/perfutil.jl @@ -1,6 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -using Printf +using Printf, Random const mintrials = 5 const mintime = 2000.0 diff --git a/test/perf/simd/sum_reduce.jl b/test/perf/simd/sum_reduce.jl index 9ad02e7c9fb7a..b6bf05dbebc54 100644 --- a/test/perf/simd/sum_reduce.jl +++ b/test/perf/simd/sum_reduce.jl @@ -23,4 +23,3 @@ for t in [Float32,Float64] bits = 8*sizeof(t) @timeit(flog_sum_reduce(100,x), "sum_reduction_$bits", "SIMD sum reduction over array of type $t", "SIMD") end - diff --git a/test/pkg.jl b/test/pkg.jl index 3f2dee5d118b2..99b0294ce5d40 100644 --- a/test/pkg.jl +++ b/test/pkg.jl @@ -1,6 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license import Base.Pkg.PkgError +using Random: randstring function capture_stdout(f::Function) let fname = tempname() diff --git a/test/ranges.jl b/test/ranges.jl index 4df601e4e981b..8acb419dce83f 100644 --- a/test/ranges.jl +++ b/test/ranges.jl @@ -1,6 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -using Dates +using Dates, Random # Compare precision in a manner sensitive to subnormals, which lose # precision compared to widening. diff --git a/test/read.jl b/test/read.jl index b6b6fef9bebd2..abf3c00091992 100644 --- a/test/read.jl +++ b/test/read.jl @@ -1,6 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license using DelimitedFiles +using Random mktempdir() do dir diff --git a/test/reduce.jl b/test/reduce.jl index 642df03d6452c..66ac705fab643 100644 --- a/test/reduce.jl +++ b/test/reduce.jl @@ -1,5 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license +using Random + # fold(l|r) & mapfold(l|r) @test foldl(+, Int64[]) === Int64(0) # In reference to issues #7465/#20144 (PR #20160) @test foldl(+, Int16[]) === Int16(0) # In reference to issues #21536 diff --git a/test/reducedim.jl b/test/reducedim.jl index 452582497f194..53435f454d464 100644 --- a/test/reducedim.jl +++ b/test/reducedim.jl @@ -1,5 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license +using Random + # main tests function safe_mapslices(op, A, region) diff --git a/test/reflection.jl b/test/reflection.jl index b5dfd45d87155..e22c553c03771 100644 --- a/test/reflection.jl +++ b/test/reflection.jl @@ -5,7 +5,7 @@ # sufficient to catch segfault bugs. module ReflectionTest -using Test +using Test, Random function test_ast_reflection(freflect, f, types) @test !isempty(freflect(f, types)) @@ -55,7 +55,7 @@ end # module ReflectionTest # code_warntype module WarnType -using Test +using Test, Random function warntype_hastag(f, types, tag) iob = IOBuffer() @@ -782,7 +782,7 @@ test_similar_codeinfo(cinfo, cinfo_generated) @test_throws ErrorException code_lowered(f22979, typeof.(x22979), false) module MethodDeletion -using Test +using Test, Random # Deletion after compiling top-level call bar1(x) = 1 diff --git a/test/repl.jl b/test/repl.jl index e0aa274cf46c1..ae39f0cf2b9a6 100644 --- a/test/repl.jl +++ b/test/repl.jl @@ -7,6 +7,7 @@ include("testenv.jl") isdefined(Main, :TestHelpers) || @eval Main include(joinpath(dirname(@__FILE__), "TestHelpers.jl")) using Main.TestHelpers import Base: REPL, LineEdit +using Random function fake_repl(f; options::REPL.Options=REPL.Options(confirm_exit=false)) # Use pipes so we can easily do blocking reads diff --git a/test/replcompletions.jl b/test/replcompletions.jl index 116296ba3bb42..66feeada0ba96 100644 --- a/test/replcompletions.jl +++ b/test/replcompletions.jl @@ -1,9 +1,12 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license using Base.REPLCompletions +using Random let ex = quote module CompletionFoo + using Random + mutable struct Test_y yy end @@ -51,7 +54,7 @@ let ex = quote test5(x::Float64) = pass const a=x->x test6()=[a, a] - test7() = rand() > 0.5 ? 1 : 1.0 + test7() = rand(Bool) ? 1 : 1.0 test8() = Any[1][1] kwtest(; x=1, y=2, w...) = pass diff --git a/test/replutil.jl b/test/replutil.jl index d30a458b359cb..596d8aa9e4ec2 100644 --- a/test/replutil.jl +++ b/test/replutil.jl @@ -1,5 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license +using Random + # For curmod_* include("testenv.jl") diff --git a/test/serialize.jl b/test/serialize.jl index 5b2aa48c92826..456d08fbbe3e7 100644 --- a/test/serialize.jl +++ b/test/serialize.jl @@ -1,6 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -using Test +using Test, Random # Check that serializer hasn't gone out-of-frame @test Serializer.sertag(Symbol) == 1 diff --git a/test/socket.jl b/test/socket.jl index fddde93284e4b..63c459a3c3751 100644 --- a/test/socket.jl +++ b/test/socket.jl @@ -1,5 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license +using Random + @testset "parsing" begin @test ip"127.0.0.1" == IPv4(127,0,0,1) @test ip"192.0" == IPv4(192,0,0,0) diff --git a/test/sorting.jl b/test/sorting.jl index e8fc91ce94a5d..427a8c902b911 100644 --- a/test/sorting.jl +++ b/test/sorting.jl @@ -1,6 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license using Base.Order: Forward +using Random @test sort([2,3,1]) == [1,2,3] @test sort([2,3,1], rev=true) == [3,2,1] diff --git a/test/spawn.jl b/test/spawn.jl index dd33d032a7309..4921b2d0cb072 100644 --- a/test/spawn.jl +++ b/test/spawn.jl @@ -4,6 +4,8 @@ # Cross Platform tests for spawn. # ################################### +using Random + valgrind_off = ccall(:jl_running_on_valgrind, Cint, ()) == 0 yescmd = `yes` diff --git a/test/staged.jl b/test/staged.jl index 1c691d9e7db6f..dad910a1b3a5e 100644 --- a/test/staged.jl +++ b/test/staged.jl @@ -1,6 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license using Base.Printf: @sprintf +using Random @generated function staged_t1(a,b) if a == Int @@ -140,7 +141,7 @@ end # @generated functions that throw (shouldn't segfault or throw) module TestGeneratedThrow - using Test + using Test, Random @generated function bar(x) error("I'm not happy with type $x") diff --git a/test/statistics.jl b/test/statistics.jl index ad8a357a09983..9c043967846d3 100644 --- a/test/statistics.jl +++ b/test/statistics.jl @@ -1,6 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -using Test +using Test, Random @testset "middle" begin @test middle(3) === 3.0 diff --git a/test/strings/basic.jl b/test/strings/basic.jl index 0d92f69292fa5..aa5125554ab7f 100644 --- a/test/strings/basic.jl +++ b/test/strings/basic.jl @@ -1,5 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license +using Random + @testset "constructors" begin @test String([0x61,0x62,0x63,0x21]) == "abc!" @test String("abc!") == "abc!" diff --git a/test/subarray.jl b/test/subarray.jl index 3a1917ac9367c..211e6bd24563e 100644 --- a/test/subarray.jl +++ b/test/subarray.jl @@ -1,6 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -using Test +using Test, Random ######## Utilities ########### diff --git a/test/syntax.jl b/test/syntax.jl index 5f05074809edc..327a663e0eb32 100644 --- a/test/syntax.jl +++ b/test/syntax.jl @@ -2,6 +2,8 @@ # tests for parser and syntax lowering +using Random + import Base.Meta.ParseError function parseall(str) diff --git a/test/testdefs.jl b/test/testdefs.jl index c6af76df2b8c8..e040988651266 100644 --- a/test/testdefs.jl +++ b/test/testdefs.jl @@ -1,6 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license -using Test +using Test, Random function runtests(name, path, isolate=true; seed=nothing) old_print_setting = Test.TESTSET_PRINT_ENABLE[] @@ -14,7 +14,7 @@ function runtests(name, path, isolate=true; seed=nothing) else m = Main end - @eval(m, using Test) + @eval(m, using Test, Random) ex = quote @timed @testset $"$name" begin # srand(nothing) will fail diff --git a/test/version.jl b/test/version.jl index e45639fe16c69..13d6309e15364 100644 --- a/test/version.jl +++ b/test/version.jl @@ -1,5 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license +using Random + # parsing tests @test v"2" == VersionNumber(2) @test v"3.2" == VersionNumber(3, 2)