Skip to content

Commit

Permalink
move Random to stdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
rfourquet committed Dec 14, 2017
1 parent 5a2ef5f commit 10eb971
Show file tree
Hide file tree
Showing 125 changed files with 463 additions and 312 deletions.
2 changes: 2 additions & 0 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,8 @@ See also [`checkindex`](@ref).
# Examples
```jldoctest
julia> using Random
julia> A = rand(3, 3);
julia> checkbounds(Bool, A, 2)
Expand Down
32 changes: 26 additions & 6 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1120,12 +1120,6 @@ end
import .LinAlg: cond
@deprecate cond(F::LinAlg.LU, p::Integer) cond(convert(AbstractArray, F), p)

# PR #21359
import .Random: srand
@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))))

# PR #21974
@deprecate versioninfo(verbose::Bool) versioninfo(verbose=verbose)
@deprecate versioninfo(io::IO, verbose::Bool) versioninfo(io, verbose=verbose)
Expand Down Expand Up @@ -2974,6 +2968,32 @@ end
# issue #24822
@deprecate_binding Display AbstractDisplay

# PR #24874
@deprecate_moved rand "Random" true true
@deprecate_moved rand! "Random" true true
@deprecate_moved srand "Random" true true
@deprecate_moved AbstractRNG "Random" true true
@deprecate_moved sprand "Random" true true
@deprecate_moved sprandn "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 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" true true

# 24595
@deprecate falses(A::AbstractArray) falses(size(A))
@deprecate trues(A::AbstractArray) trues(size(A))
Expand Down
2 changes: 2 additions & 0 deletions base/docs/basedocs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,8 @@ callable with no arguments). The task exits when this function returns.
# Examples
```jldoctest
julia> using Random
julia> a() = det(rand(1000, 1000));
julia> b = Task(a);
Expand Down
2 changes: 1 addition & 1 deletion base/error.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,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 + (Crand(Float64) * 2.0 * ebo.jitter)))
(curr_delay, (next_n, next_delay))
end
done(ebo::ExponentialBackOff, state) = state[1]<1
Expand Down
2 changes: 2 additions & 0 deletions base/essentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,8 @@ if the index is out of bounds, or has an undefined reference.
# Examples
```jldoctest
julia> using Random
julia> isassigned(rand(3, 3), 5)
true
Expand Down
2 changes: 2 additions & 0 deletions base/event.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ If a second argument `val` is provided, it will be passed to the task (via the r
the woken task.
```jldoctest
julia> using Random
julia> a5() = det(rand(1000, 1000));
julia> b = Task(a5);
Expand Down
23 changes: 0 additions & 23 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -490,12 +490,6 @@ export
prod!,
prod,
promote_shape,
randcycle,
randcycle!,
randperm,
randperm!,
randsubseq!,
randsubseq,
range,
reducedim,
repmat,
Expand Down Expand Up @@ -744,7 +738,6 @@ export
print_shortest,
print_with_color,
println,
randstring,
repeat,
replace,
repr,
Expand All @@ -769,20 +762,6 @@ export
unescape_string,
warn,

# random numbers
AbstractRNG,
MersenneTwister,
RandomDevice,
rand!,
rand,
randn!,
randn,
randexp!,
randexp,
srand,
bitrand,
randjump,

# bigfloat & precision
precision,
rounding,
Expand Down Expand Up @@ -1234,8 +1213,6 @@ export
sparsevec,
spdiagm,
spones,
sprand,
sprandn,
spzeros,
rowvals,
nzrange,
Expand Down
2 changes: 1 addition & 1 deletion base/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ function mktemp(parent=tempdir())
end

function mktempdir(parent=tempdir())
seed::UInt32 = rand(UInt32)
seed::UInt32 = Crand(UInt32)
while true
if (seed & typemax(UInt16)) == 0
seed += 1
Expand Down
2 changes: 2 additions & 0 deletions base/multimedia.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ corresponding [`show`](@ref) method for `typeof(x)`.)
# Examples
```jldoctest
julia> using Random
julia> mimewritable(MIME("text/plain"), rand(5))
true
Expand Down
2 changes: 2 additions & 0 deletions base/number.jl
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ julia> zero(1)
julia> zero(big"2.0")
0.0
julia> using Random
julia> zero(rand(2,2))
2×2 Array{Float64,2}:
0.0 0.0
Expand Down
2 changes: 2 additions & 0 deletions base/permuteddimsarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ See also: [`permutedims`](@ref).
# Examples
```jldoctest
julia> using Random
julia> A = rand(3,5,4);
julia> B = PermutedDimsArray(A, (3,1,2));
Expand Down
10 changes: 0 additions & 10 deletions base/serialize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 0 additions & 6 deletions base/sparse/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -608,12 +608,6 @@ function normestinv(A::SparseMatrixCSC{T}, t::Integer = min(2,maximum(size(A))))

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

function _rand_pm1!(v)
for i in eachindex(v)
v[i] = rand()<0.5 ? 1 : -1
end
end

function _any_abs_eq(v,n::Int)
for vv in v
if abs(vv)==n
Expand Down
4 changes: 2 additions & 2 deletions base/sparse/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ import Base: @get!, acos, acosd, acot, acotd, acsch, asech, asin, asind, asinh,
vcat, hcat, hvcat, cat, imag, indmax, ishermitian, kron, length, log, log1p, max, min,
maximum, minimum, norm, one, promote_eltype, real, reshape, rot180,
rotl90, rotr90, round, scale!, setindex!, similar, size, transpose, tril,
triu, vec, permute!, map, map!
triu, vec, permute!, map, map!, _rand_pm1!

export AbstractSparseArray, AbstractSparseMatrix, AbstractSparseVector,
SparseMatrixCSC, SparseVector, blkdiag, droptol!, dropzeros!, dropzeros,
issparse, nonzeros, nzrange, rowvals, sparse, sparsevec, spdiagm, spones,
sprand, sprandn, spzeros, nnz, permute
spzeros, nnz, permute

include("abstractsparse.jl")
include("sparsematrix.jl")
Expand Down
113 changes: 0 additions & 113 deletions base/sparse/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1318,119 +1318,6 @@ function findnz(S::SparseMatrixCSC{Tv,Ti}) where {Tv,Ti}
return (I, J, V)
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]"))
N = n*m

I, J = Vector{Int}(), Vector{Int}() # indices of nonzero elements
sizehint!(I, round(Int,N*density))
sizehint!(J, round(Int,N*density))

# density of nonzero columns:
L = log1p(-density)
coldensity = -expm1(m*L) # = 1 - (1-density)^m
colsparsity = exp(m*L) # = 1 - coldensity
iL = 1/L

rows = Vector{Int}()
for j in randsubseq(r, 1:n, coldensity)
# To get the right statistics, we *must* have a nonempty column j
# even if p*m << 1. To do this, we use an approach similar to
# the one in randsubseq to compute the expected first nonzero row k,
# except given that at least one is nonzero (via Bayes' rule);
# carefully rearranged to avoid excessive roundoff errors.
k = ceil(log(colsparsity + rand(r)*coldensity) * iL)
ik = k < 1 ? 1 : k > m ? m : Int(k) # roundoff-error/underflow paranoia
randsubseq!(r, rows, 1:m-ik, density)
push!(rows, m-ik+1)
append!(I, rows)
nrows = length(rows)
Jlen = length(J)
resize!(J, Jlen+nrows)
@inbounds for i = Jlen+1:length(J)
J[i] = j
end
end
I, J
end

"""
sprand([rng],[type],m,[n],p::AbstractFloat,[rfn])
Create a random length `m` sparse vector or `m` by `n` sparse matrix, in
which the probability of any element being nonzero is independently given by
`p` (and hence the mean density of nonzeros is also exactly `p`). Nonzero
values are sampled from the distribution specified by `rfn` and have the type `type`. The uniform
distribution is used in case `rfn` is not specified. The optional `rng`
argument specifies a random number generator, see [Random Numbers](@ref).
# Examples
```jldoctest
julia> rng = MersenneTwister(1234);
julia> sprand(rng, Bool, 2, 2, 0.5)
2×2 SparseMatrixCSC{Bool,Int64} with 2 stored entries:
[1, 1] = true
[2, 1] = true
julia> sprand(rng, Float64, 3, 0.75)
3-element SparseVector{Float64,Int64} with 1 stored entry:
[3] = 0.298614
```
"""
function sprand(r::AbstractRNG, m::Integer, n::Integer, density::AbstractFloat,
rfn::Function, ::Type{T}=eltype(rfn(r,1))) where T
N = m*n
N == 0 && return spzeros(T,m,n)
N == 1 && return rand(r) <= density ? sparse([1], [1], rfn(r,1)) : spzeros(T,1,1)

I,J = sprand_IJ(r, m, n, density)
sparse_IJ_sorted!(I, J, rfn(r,length(I)), m, n, +) # it will never need to combine
end

function sprand(m::Integer, n::Integer, density::AbstractFloat,
rfn::Function, ::Type{T}=eltype(rfn(1))) where T
N = m*n
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)
sparse_IJ_sorted!(I, J, rfn(length(I)), m, n, +) # it will never need to combine
end

truebools(r::AbstractRNG, n::Integer) = ones(Bool, n)

sprand(m::Integer, n::Integer, density::AbstractFloat) = sprand(GLOBAL_RNG,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)

"""
sprandn([rng], m[,n],p::AbstractFloat)
Create a random sparse vector of length `m` or sparse matrix of size `m` by `n`
with the specified (independent) probability `p` of any entry being nonzero,
where nonzero values are sampled from the normal distribution. The optional `rng`
argument specifies a random number generator, see [Random Numbers](@ref).
# Examples
```jldoctest
julia> rng = MersenneTwister(1234);
julia> sprandn(rng, 2, 2, 0.75)
2×2 SparseMatrixCSC{Float64,Int64} with 3 stored entries:
[1, 1] = 0.532813
[2, 1] = -0.271735
[2, 2] = 0.502334
```
"""
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)

"""
spones(S)
Expand Down
25 changes: 0 additions & 25 deletions base/sparse/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -477,31 +477,6 @@ copy!(A::SparseMatrixCSC, B::SparseVector{TvB,TiB}) where {TvB,TiB} =
copy!(A, SparseMatrixCSC{TvB,TiB}(B.n, 1, TiB[1, length(B.nzind)+1], B.nzind, B.nzval))


### Rand Construction
sprand(n::Integer, p::AbstractFloat, rfn::Function, ::Type{T}) where {T} = sprand(GLOBAL_RNG, 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)
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(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)

sprandn(n::Integer, p::AbstractFloat) = sprand(GLOBAL_RNG, n, p, randn)
sprandn(r::AbstractRNG, n::Integer, p::AbstractFloat) = sprand(r, n, p, randn)

## Indexing into Matrices can return SparseVectors

# Column slices
Expand Down
Loading

0 comments on commit 10eb971

Please sign in to comment.