Skip to content

Commit

Permalink
move Random to stdlib (#24874)
Browse files Browse the repository at this point in the history
  • Loading branch information
rfourquet authored Jan 15, 2018
1 parent 9924f79 commit 60cd7cf
Show file tree
Hide file tree
Showing 118 changed files with 479 additions and 227 deletions.
38 changes: 22 additions & 16 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion base/error.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 4 additions & 21 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -446,12 +446,6 @@ export
prod!,
prod,
promote_shape,
randcycle,
randcycle!,
randperm,
randperm!,
randsubseq!,
randsubseq,
range,
reducedim,
repmat,
Expand Down Expand Up @@ -723,7 +717,6 @@ export
print_shortest,
print_with_color,
println,
randstring,
repeat,
replace,
replace!,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -1117,6 +1096,10 @@ export
unsafe_store!,
unsafe_write,

# implemented in Random module
rand,
randn,

# Macros
# parser internal
@__FILE__,
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 = Base.Crand(UInt32)
while true
if (seed & typemax(UInt16)) == 0
seed += 1
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
45 changes: 34 additions & 11 deletions base/sysimg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand All @@ -494,27 +515,29 @@ 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"
@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 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"
Expand Down
8 changes: 0 additions & 8 deletions doc/src/base/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ Base.similar(::AbstractArray)
Base.similar(::Any, ::Tuple)
Base.linspace
Base.logspace
Base.Random.randsubseq
Base.Random.randsubseq!
```

## Basic functions
Expand Down Expand Up @@ -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!
Expand Down
1 change: 1 addition & 0 deletions doc/src/base/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@
* [Sparse Arrays](@ref)
* [Iterative Eigensolvers](@ref lib-itereigen)
* [Printf](@ref)
* [Random Numbers](@ref)
35 changes: 0 additions & 35 deletions doc/src/base/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
```
1 change: 0 additions & 1 deletion doc/src/base/strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ Base.chomp
Base.thisind
Base.nextind
Base.prevind
Base.Random.randstring
Base.textwidth
Base.isalpha
Base.isascii
Expand Down
5 changes: 3 additions & 2 deletions doc/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion stdlib/Base64/test/runtests.jl
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
3 changes: 1 addition & 2 deletions stdlib/CRC32c/test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Test
using Test, Random
using CRC32c

function test_crc32c(crc32c)
Expand Down Expand Up @@ -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)

2 changes: 1 addition & 1 deletion stdlib/DelimitedFiles/test/runtests.jl
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 2 additions & 0 deletions stdlib/Distributed/src/cluster.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions stdlib/Distributed/test/distributed_exec.jl
Original file line number Diff line number Diff line change
@@ -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"))
Expand All @@ -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))]
Expand Down Expand Up @@ -1498,4 +1498,3 @@ end
# cluster at any time only supports a single topology.
rmprocs(workers())
include("topology.jl")

2 changes: 2 additions & 0 deletions stdlib/Distributed/test/topology.jl
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
Loading

0 comments on commit 60cd7cf

Please sign in to comment.