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 1, 2017
1 parent dba7e57 commit a4a3785
Show file tree
Hide file tree
Showing 24 changed files with 127 additions and 117 deletions.
6 changes: 0 additions & 6 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1127,12 +1127,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
3 changes: 2 additions & 1 deletion base/distributed/cluster.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1123,7 +1123,8 @@ function init_parallel()
global PGRP
global LPROC
LPROC.id = 1
cluster_cookie(randstring(HDR_COOKIE_LEN))
# FIXME: replace back by randstring(HDR_COOKIE_LEN)
cluster_cookie("c03rgKi1joVnWq9l")
assert(isempty(PGRP.workers))
register_worker(LPROC)
end
Expand Down
15 changes: 0 additions & 15 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -491,12 +491,6 @@ export
prod!,
prod,
promote_shape,
randcycle,
randcycle!,
randperm,
randperm!,
randsubseq!,
randsubseq,
range,
reducedim,
repmat,
Expand Down Expand Up @@ -764,7 +758,6 @@ export
print_shortest,
print_with_color,
println,
randstring,
repeat,
replace,
repr,
Expand Down Expand Up @@ -795,17 +788,9 @@ export

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

# bigfloat & precision
precision,
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
11 changes: 6 additions & 5 deletions base/sparse/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +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]"))
Expand Down Expand Up @@ -1356,6 +1355,8 @@ function sprand_IJ(r::AbstractRNG, m::Integer, n::Integer, density::AbstractFloa
I, J
end

using Base: defaultRNG, randsubseq!, randsubseq, randn

"""
sprand([rng],[type],m,[n],p::AbstractFloat,[rfn])
Expand Down Expand Up @@ -1396,18 +1397,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) = ones(Bool, 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)
Expand All @@ -1429,7 +1430,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)

"""
spones(S)
Expand Down
10 changes: 5 additions & 5 deletions base/sparse/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -477,28 +477,28 @@ copy!(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
Expand Down
20 changes: 14 additions & 6 deletions base/sysimg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,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")
using .Printf
Expand Down Expand Up @@ -405,6 +399,18 @@ include("libgit2/libgit2.jl")
# package manager
include("pkg/pkg.jl")

# The following is used in sparse code
abstract type AbstractRNG end
function defaultRNG end
function randn end
function randsubseq end
function randsubseq! end

# we want rand & rand! available by default in Base
function rand end
function rand! end
function srand end

# sparse matrices, vectors, and sparse linear algebra
include("sparse/sparse.jl")
using .SparseArrays
Expand Down Expand Up @@ -478,6 +484,7 @@ Base.require(:FileWatching)
Base.require(:IterativeEigenSolvers)
Base.require(:Mmap)
Base.require(:Profile)
Base.require(:Random)
Base.require(:SharedArrays)
Base.require(:SuiteSparse)
Base.require(:Test)
Expand All @@ -487,6 +494,7 @@ Base.require(:Test)
@deprecate_binding Mmap root_module(:Mmap) true ", run `using Mmap` instead"
@deprecate_binding Profile root_module(:Profile) true ", run `using Profile` instead"
@deprecate_binding Dates root_module(:Dates) true ", run `using Dates` instead"
@deprecate_binding Random root_module(:Random) true ", run `using Random` instead"
end

empty!(LOAD_PATH)
Expand Down
5 changes: 4 additions & 1 deletion doc/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ if Sys.iswindows()
cp_q("../stdlib/CRC32c/docs/src/index.md", "src/stdlib/crc32c.md")
cp_q("../stdlib/Dates/docs/src/index.md", "src/stdlib/dates.md")
cp_q("../stdlib/IterativeEigenSolvers/docs/src/index.md", "src/stdlib/iterativeeigensolvers.md")
cp_q("../stdlib/Random/docs/src/index.md", "src/stdlib/random.md")
else
symlink_q("../../../stdlib/DelimitedFiles/docs/src/index.md", "src/stdlib/delimitedfiles.md")
symlink_q("../../../stdlib/Test/docs/src/index.md", "src/stdlib/test.md")
Expand All @@ -42,6 +43,7 @@ else
symlink_q("../../../stdlib/CRC32c/docs/src/index.md", "src/stdlib/crc32c.md")
symlink_q("../../../stdlib/Dates/docs/src/index.md", "src/stdlib/dates.md")
symlink_q("../../../stdlib/IterativeEigenSolvers/docs/src/index.md", "src/stdlib/iterativeeigensolvers.md")
symlink_q("../../../stdlib/Random/docs/src/index.md", "src/stdlib/random.md")
end

const PAGES = [
Expand Down Expand Up @@ -117,6 +119,7 @@ const PAGES = [
"stdlib/filewatching.md",
"stdlib/crc32c.md",
"stdlib/iterativeeigensolvers.md",
"stdlib/random.md",
],
"Developer Documentation" => [
"devdocs/reflection.md",
Expand Down Expand Up @@ -152,7 +155,7 @@ const PAGES = [
]

using DelimitedFiles, Test, Mmap, SharedArrays, Profile, Base64, FileWatching, CRC32c,
Dates, IterativeEigenSolvers
Dates, IterativeEigenSolvers, Random

makedocs(
build = joinpath(pwd(), "_build/html/en"),
Expand Down
2 changes: 1 addition & 1 deletion doc/src/stdlib/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ base64.md
filewatching.md
crc32c.md
dates.md

random.md
35 changes: 0 additions & 35 deletions doc/src/stdlib/numbers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
34 changes: 34 additions & 0 deletions stdlib/Random/docs/src/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 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
```
10 changes: 10 additions & 0 deletions base/random/RNGs.jl → stdlib/Random/src/RNGs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 16 additions & 5 deletions base/random/random.jl → stdlib/Random/src/Random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

module Random

using Base.dSFMT
include("dSFMT.jl")

using .dSFMT
using Base.GMP: Limb, MPZ
import Base: copymutable, copy, copy!, ==, hash
import Base: rand, rand!, srand, defaultRNG, randn, randsubseq, randsubseq!,
copymutable, copy, copy!, ==, hash, serialize, deserialize

export srand,
rand, rand!,
Expand All @@ -17,13 +20,12 @@ export srand,
randperm, randperm!,
randcycle, randcycle!,
AbstractRNG, MersenneTwister, RandomDevice,
GLOBAL_RNG, randjump
GLOBAL_RNG, defaultRNG, randjump

defaultRNG() = GLOBAL_RNG

## general definitions

abstract type AbstractRNG end

### floats

abstract type FloatInterval{T<:AbstractFloat} end
Expand Down Expand Up @@ -271,4 +273,13 @@ true
"""
srand(rng::AbstractRNG, ::Void) = srand(rng)


## deprecations

# 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))))

end # module
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit a4a3785

Please sign in to comment.