Skip to content

Commit 132d491

Browse files
committed
move Random to stdlib
1 parent 9cdd887 commit 132d491

File tree

118 files changed

+492
-356
lines changed

Some content is hidden

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

118 files changed

+492
-356
lines changed

base/deprecated.jl

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -199,20 +199,6 @@ end
199199
import .LinAlg: cond
200200
@deprecate cond(F::LinAlg.LU, p::Integer) cond(convert(AbstractArray, F), p)
201201

202-
# PR #21359
203-
import .Random: srand, randjump
204-
205-
@deprecate srand(r::MersenneTwister, filename::AbstractString, n::Integer=4) srand(r, read!(filename, Vector{UInt32}(uninitialized, Int(n))))
206-
@deprecate srand(filename::AbstractString, n::Integer=4) srand(read!(filename, Vector{UInt32}(uninitialized, Int(n))))
207-
@deprecate MersenneTwister(filename::AbstractString) srand(MersenneTwister(0), read!(filename, Vector{UInt32}(uninitialized, Int(4))))
208-
209-
function randjump(mt::MersenneTwister, jumps::Integer, jumppoly::AbstractString)
210-
depwarn("`randjump(rng, jumps, jumppoly::AbstractString)` is deprecated; use `randjump(rng, steps, jumps)` instead", :randjump)
211-
Base.Random._randjump(mt, dSFMT.GF2X(jumppoly), jumps)
212-
end
213-
214-
@deprecate randjump(mt::MersenneTwister, jumps::Integer) randjump(mt, big(10)^20, jumps)
215-
216202
# PR #21974
217203
@deprecate versioninfo(verbose::Bool) versioninfo(verbose=verbose)
218204
@deprecate versioninfo(io::IO, verbose::Bool) versioninfo(io, verbose=verbose)
@@ -658,8 +644,6 @@ end
658644
@deprecate convert(::Type{S}, g::Unicode.GraphemeIterator) where {S<:AbstractString} convert(S, g.s)
659645
@deprecate convert(::Type{String}, v::AbstractVector{Char}) String(v)
660646

661-
@deprecate convert(::Type{UInt128}, u::Random.UUID) UInt128(u)
662-
@deprecate convert(::Type{Random.UUID}, s::AbstractString) Random.UUID(s)
663647
@deprecate convert(::Type{Libc.FILE}, s::IO) Libc.FILE(s)
664648
@deprecate convert(::Type{VersionNumber}, v::Integer) VersionNumber(v)
665649
@deprecate convert(::Type{VersionNumber}, v::Tuple) VersionNumber(v)
@@ -2550,6 +2534,28 @@ end
25502534
# issue #24822
25512535
@deprecate_binding Display AbstractDisplay
25522536

2537+
# PR #24874
2538+
@deprecate_moved rand! "Random" true true
2539+
@deprecate_moved srand "Random" true true
2540+
@deprecate_moved AbstractRNG "Random" true true
2541+
@deprecate_moved randcycle "Random" true true
2542+
@deprecate_moved randcycle! "Random" true true
2543+
@deprecate_moved randperm "Random" true true
2544+
@deprecate_moved randperm! "Random" true true
2545+
@deprecate_moved shuffle "Random" true true
2546+
@deprecate_moved shuffle! "Random" true true
2547+
@deprecate_moved randsubseq "Random" true true
2548+
@deprecate_moved randsubseq! "Random" true true
2549+
@deprecate_moved randstring "Random" true true
2550+
@deprecate_moved MersenneTwister "Random" true true
2551+
@deprecate_moved RandomDevice "Random" true true
2552+
@deprecate_moved randn! "Random" true true
2553+
@deprecate_moved randexp "Random" true true
2554+
@deprecate_moved randexp! "Random" true true
2555+
@deprecate_moved bitrand "Random" true true
2556+
@deprecate_moved randjump "Random" true true
2557+
@deprecate_moved GLOBAL_RNG "Random" false true
2558+
25532559
# 24595
25542560
@deprecate falses(A::AbstractArray) falses(size(A))
25552561
@deprecate trues(A::AbstractArray) trues(size(A))

base/error.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ start(ebo::ExponentialBackOff) = (ebo.n, min(ebo.first_delay, ebo.max_delay))
172172
function next(ebo::ExponentialBackOff, state)
173173
next_n = state[1]-1
174174
curr_delay = state[2]
175-
next_delay = min(ebo.max_delay, state[2] * ebo.factor * (1.0 - ebo.jitter + (rand() * 2.0 * ebo.jitter)))
175+
next_delay = min(ebo.max_delay, state[2] * ebo.factor * (1.0 - ebo.jitter + (rand(Float64) * 2.0 * ebo.jitter)))
176176
(curr_delay, (next_n, next_delay))
177177
end
178178
done(ebo::ExponentialBackOff, state) = state[1]<1

base/exports.jl

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -449,12 +449,6 @@ export
449449
prod!,
450450
prod,
451451
promote_shape,
452-
randcycle,
453-
randcycle!,
454-
randperm,
455-
randperm!,
456-
randsubseq!,
457-
randsubseq,
458452
range,
459453
reducedim,
460454
repmat,
@@ -719,7 +713,6 @@ export
719713
print_shortest,
720714
print_with_color,
721715
println,
722-
randstring,
723716
repeat,
724717
replace,
725718
replace!,
@@ -747,20 +740,6 @@ export
747740
@warn,
748741
@error,
749742

750-
# random numbers
751-
AbstractRNG,
752-
MersenneTwister,
753-
RandomDevice,
754-
rand!,
755-
rand,
756-
randn!,
757-
randn,
758-
randexp!,
759-
randexp,
760-
srand,
761-
bitrand,
762-
randjump,
763-
764743
# bigfloat & precision
765744
precision,
766745
rounding,
@@ -1197,9 +1176,13 @@ export
11971176
sparse,
11981177
sparsevec,
11991178
spdiagm,
1200-
sprand,
1201-
sprandn,
12021179
spzeros,
12031180
rowvals,
12041181
nzrange,
1205-
nnz
1182+
nnz,
1183+
sprand,
1184+
sprandn,
1185+
1186+
# implemented in Random module
1187+
rand,
1188+
randn

base/file.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ function mktemp(parent=tempdir())
285285
end
286286

287287
function mktempdir(parent=tempdir())
288-
seed::UInt32 = rand(UInt32)
288+
seed::UInt32 = Base.Crand(UInt32)
289289
while true
290290
if (seed & typemax(UInt16)) == 0
291291
seed += 1

base/serialize.jl

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,14 +1195,4 @@ function deserialize(s::AbstractSerializer, t::Type{Regex})
11951195
Regex(pattern, compile_options, match_options)
11961196
end
11971197

1198-
if !Sys.iswindows()
1199-
function serialize(s::AbstractSerializer, rd::RandomDevice)
1200-
serialize_type(s, typeof(rd))
1201-
serialize(s, rd.unlimited)
1202-
end
1203-
function deserialize(s::AbstractSerializer, t::Type{RandomDevice})
1204-
unlimited = deserialize(s)
1205-
return RandomDevice(unlimited)
1206-
end
1207-
end
12081198
end

base/sparse/linalg.jl

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -601,12 +601,6 @@ function normestinv(A::SparseMatrixCSC{T}, t::Integer = min(2,maximum(size(A))))
601601

602602
S = zeros(T <: Real ? Int : Ti, n, t)
603603

604-
function _rand_pm1!(v)
605-
for i in eachindex(v)
606-
v[i] = rand()<0.5 ? 1 : -1
607-
end
608-
end
609-
610604
function _any_abs_eq(v,n::Int)
611605
for vv in v
612606
if abs(vv)==n

base/sparse/sparse.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ import Base: @get!, acos, acosd, acot, acotd, acsch, asech, asin, asind, asinh,
2424
vcat, hcat, hvcat, cat, imag, indmax, ishermitian, kron, length, log, log1p, max, min,
2525
maximum, minimum, norm, one, promote_eltype, real, reshape, rot180,
2626
rotl90, rotr90, round, scale!, setindex!, similar, size, transpose, tril,
27-
triu, vec, permute!, map, map!, Array
27+
triu, vec, permute!, map, map!, Array, _rand_pm1!
2828

2929
export AbstractSparseArray, AbstractSparseMatrix, AbstractSparseVector,
3030
SparseMatrixCSC, SparseVector, blkdiag, droptol!, dropzeros!, dropzeros,
3131
issparse, nonzeros, nzrange, rowvals, sparse, sparsevec, spdiagm,
32-
sprand, sprandn, spzeros, nnz, permute
32+
spzeros, nnz, permute
3333

3434
include("abstractsparse.jl")
3535
include("sparsematrix.jl")

base/sparse/sparsematrix.jl

Lines changed: 0 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,118 +1353,6 @@ function _sparse_findprevnz(m::SparseMatrixCSC, i::Integer)
13531353
return LinearIndices(m)[m.rowval[prevhi-1], prevcol]
13541354
end
13551355

1356-
import Base.Random.GLOBAL_RNG
1357-
function sprand_IJ(r::AbstractRNG, m::Integer, n::Integer, density::AbstractFloat)
1358-
((m < 0) || (n < 0)) && throw(ArgumentError("invalid Array dimensions"))
1359-
0 <= density <= 1 || throw(ArgumentError("$density not in [0,1]"))
1360-
N = n*m
1361-
1362-
I, J = Vector{Int}(), Vector{Int}() # indices of nonzero elements
1363-
sizehint!(I, round(Int,N*density))
1364-
sizehint!(J, round(Int,N*density))
1365-
1366-
# density of nonzero columns:
1367-
L = log1p(-density)
1368-
coldensity = -expm1(m*L) # = 1 - (1-density)^m
1369-
colsparsity = exp(m*L) # = 1 - coldensity
1370-
iL = 1/L
1371-
1372-
rows = Vector{Int}()
1373-
for j in randsubseq(r, 1:n, coldensity)
1374-
# To get the right statistics, we *must* have a nonempty column j
1375-
# even if p*m << 1. To do this, we use an approach similar to
1376-
# the one in randsubseq to compute the expected first nonzero row k,
1377-
# except given that at least one is nonzero (via Bayes' rule);
1378-
# carefully rearranged to avoid excessive roundoff errors.
1379-
k = ceil(log(colsparsity + rand(r)*coldensity) * iL)
1380-
ik = k < 1 ? 1 : k > m ? m : Int(k) # roundoff-error/underflow paranoia
1381-
randsubseq!(r, rows, 1:m-ik, density)
1382-
push!(rows, m-ik+1)
1383-
append!(I, rows)
1384-
nrows = length(rows)
1385-
Jlen = length(J)
1386-
resize!(J, Jlen+nrows)
1387-
@inbounds for i = Jlen+1:length(J)
1388-
J[i] = j
1389-
end
1390-
end
1391-
I, J
1392-
end
1393-
1394-
"""
1395-
sprand([rng],[type],m,[n],p::AbstractFloat,[rfn])
1396-
1397-
Create a random length `m` sparse vector or `m` by `n` sparse matrix, in
1398-
which the probability of any element being nonzero is independently given by
1399-
`p` (and hence the mean density of nonzeros is also exactly `p`). Nonzero
1400-
values are sampled from the distribution specified by `rfn` and have the type `type`. The uniform
1401-
distribution is used in case `rfn` is not specified. The optional `rng`
1402-
argument specifies a random number generator, see [Random Numbers](@ref).
1403-
1404-
# Examples
1405-
```jldoctest
1406-
julia> rng = MersenneTwister(1234);
1407-
1408-
julia> sprand(rng, Bool, 2, 2, 0.5)
1409-
2×2 SparseMatrixCSC{Bool,Int64} with 2 stored entries:
1410-
[1, 1] = true
1411-
[2, 1] = true
1412-
1413-
julia> sprand(rng, Float64, 3, 0.75)
1414-
3-element SparseVector{Float64,Int64} with 1 stored entry:
1415-
[3] = 0.298614
1416-
```
1417-
"""
1418-
function sprand(r::AbstractRNG, m::Integer, n::Integer, density::AbstractFloat,
1419-
rfn::Function, ::Type{T}=eltype(rfn(r,1))) where T
1420-
N = m*n
1421-
N == 0 && return spzeros(T,m,n)
1422-
N == 1 && return rand(r) <= density ? sparse([1], [1], rfn(r,1)) : spzeros(T,1,1)
1423-
1424-
I,J = sprand_IJ(r, m, n, density)
1425-
sparse_IJ_sorted!(I, J, rfn(r,length(I)), m, n, +) # it will never need to combine
1426-
end
1427-
1428-
function sprand(m::Integer, n::Integer, density::AbstractFloat,
1429-
rfn::Function, ::Type{T}=eltype(rfn(1))) where T
1430-
N = m*n
1431-
N == 0 && return spzeros(T,m,n)
1432-
N == 1 && return rand() <= density ? sparse([1], [1], rfn(1)) : spzeros(T,1,1)
1433-
1434-
I,J = sprand_IJ(GLOBAL_RNG, m, n, density)
1435-
sparse_IJ_sorted!(I, J, rfn(length(I)), m, n, +) # it will never need to combine
1436-
end
1437-
1438-
truebools(r::AbstractRNG, n::Integer) = fill(true, n)
1439-
1440-
sprand(m::Integer, n::Integer, density::AbstractFloat) = sprand(GLOBAL_RNG,m,n,density)
1441-
1442-
sprand(r::AbstractRNG, m::Integer, n::Integer, density::AbstractFloat) = sprand(r,m,n,density,rand,Float64)
1443-
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)
1444-
sprand(r::AbstractRNG, ::Type{Bool}, m::Integer, n::Integer, density::AbstractFloat) = sprand(r,m,n,density, truebools, Bool)
1445-
sprand(::Type{T}, m::Integer, n::Integer, density::AbstractFloat) where {T} = sprand(GLOBAL_RNG, T, m, n, density)
1446-
1447-
"""
1448-
sprandn([rng], m[,n],p::AbstractFloat)
1449-
1450-
Create a random sparse vector of length `m` or sparse matrix of size `m` by `n`
1451-
with the specified (independent) probability `p` of any entry being nonzero,
1452-
where nonzero values are sampled from the normal distribution. The optional `rng`
1453-
argument specifies a random number generator, see [Random Numbers](@ref).
1454-
1455-
# Examples
1456-
```jldoctest
1457-
julia> rng = MersenneTwister(1234);
1458-
1459-
julia> sprandn(rng, 2, 2, 0.75)
1460-
2×2 SparseMatrixCSC{Float64,Int64} with 3 stored entries:
1461-
[1, 1] = 0.532813
1462-
[2, 1] = -0.271735
1463-
[2, 2] = 0.502334
1464-
```
1465-
"""
1466-
sprandn(r::AbstractRNG, m::Integer, n::Integer, density::AbstractFloat) = sprand(r,m,n,density,randn,Float64)
1467-
sprandn(m::Integer, n::Integer, density::AbstractFloat) = sprandn(GLOBAL_RNG,m,n,density)
14681356

14691357
LinAlg.fillstored!(S::SparseMatrixCSC, x) = (fill!(view(S.nzval, 1:(S.colptr[S.n + 1] - 1)), x); S)
14701358

base/sparse/sparsevector.jl

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -471,31 +471,6 @@ copyto!(A::SparseMatrixCSC, B::SparseVector{TvB,TiB}) where {TvB,TiB} =
471471
copyto!(A, SparseMatrixCSC{TvB,TiB}(B.n, 1, TiB[1, length(B.nzind)+1], B.nzind, B.nzval))
472472

473473

474-
### Rand Construction
475-
sprand(n::Integer, p::AbstractFloat, rfn::Function, ::Type{T}) where {T} = sprand(GLOBAL_RNG, n, p, rfn, T)
476-
function sprand(r::AbstractRNG, n::Integer, p::AbstractFloat, rfn::Function, ::Type{T}) where T
477-
I = randsubseq(r, 1:convert(Int, n), p)
478-
V = rfn(r, T, length(I))
479-
SparseVector(n, I, V)
480-
end
481-
482-
sprand(n::Integer, p::AbstractFloat, rfn::Function) = sprand(GLOBAL_RNG, n, p, rfn)
483-
function sprand(r::AbstractRNG, n::Integer, p::AbstractFloat, rfn::Function)
484-
I = randsubseq(r, 1:convert(Int, n), p)
485-
V = rfn(r, length(I))
486-
SparseVector(n, I, V)
487-
end
488-
489-
sprand(n::Integer, p::AbstractFloat) = sprand(GLOBAL_RNG, n, p, rand)
490-
491-
sprand(r::AbstractRNG, n::Integer, p::AbstractFloat) = sprand(r, n, p, rand)
492-
sprand(r::AbstractRNG, ::Type{T}, n::Integer, p::AbstractFloat) where {T} = sprand(r, n, p, (r, i) -> rand(r, T, i))
493-
sprand(r::AbstractRNG, ::Type{Bool}, n::Integer, p::AbstractFloat) = sprand(r, n, p, truebools)
494-
sprand(::Type{T}, n::Integer, p::AbstractFloat) where {T} = sprand(GLOBAL_RNG, T, n, p)
495-
496-
sprandn(n::Integer, p::AbstractFloat) = sprand(GLOBAL_RNG, n, p, randn)
497-
sprandn(r::AbstractRNG, n::Integer, p::AbstractFloat) = sprand(r, n, p, randn)
498-
499474
## Indexing into Matrices can return SparseVectors
500475

501476
# Column slices

0 commit comments

Comments
 (0)