Skip to content

Commit 60cd7cf

Browse files
authored
move Random to stdlib (#24874)
1 parent 9924f79 commit 60cd7cf

File tree

118 files changed

+479
-227
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

+479
-227
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)
@@ -641,8 +627,6 @@ end
641627
@deprecate convert(::Type{S}, g::Unicode.GraphemeIterator) where {S<:AbstractString} convert(S, g.s)
642628
@deprecate convert(::Type{String}, v::AbstractVector{Char}) String(v)
643629

644-
@deprecate convert(::Type{UInt128}, u::Random.UUID) UInt128(u)
645-
@deprecate convert(::Type{Random.UUID}, s::AbstractString) Random.UUID(s)
646630
@deprecate convert(::Type{Libc.FILE}, s::IO) Libc.FILE(s)
647631
@deprecate convert(::Type{VersionNumber}, v::Integer) VersionNumber(v)
648632
@deprecate convert(::Type{VersionNumber}, v::Tuple) VersionNumber(v)
@@ -2338,6 +2322,28 @@ end
23382322
# issue #24822
23392323
@deprecate_binding Display AbstractDisplay
23402324

2325+
# PR #24874
2326+
@deprecate_moved rand! "Random" true true
2327+
@deprecate_moved srand "Random" true true
2328+
@deprecate_moved AbstractRNG "Random" true true
2329+
@deprecate_moved randcycle "Random" true true
2330+
@deprecate_moved randcycle! "Random" true true
2331+
@deprecate_moved randperm "Random" true true
2332+
@deprecate_moved randperm! "Random" true true
2333+
@deprecate_moved shuffle "Random" true true
2334+
@deprecate_moved shuffle! "Random" true true
2335+
@deprecate_moved randsubseq "Random" true true
2336+
@deprecate_moved randsubseq! "Random" true true
2337+
@deprecate_moved randstring "Random" true true
2338+
@deprecate_moved MersenneTwister "Random" true true
2339+
@deprecate_moved RandomDevice "Random" true true
2340+
@deprecate_moved randn! "Random" true true
2341+
@deprecate_moved randexp "Random" true true
2342+
@deprecate_moved randexp! "Random" true true
2343+
@deprecate_moved bitrand "Random" true true
2344+
@deprecate_moved randjump "Random" true true
2345+
@deprecate_moved GLOBAL_RNG "Random" false true
2346+
23412347
# 24595
23422348
@deprecate falses(A::AbstractArray) falses(size(A))
23432349
@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: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -446,12 +446,6 @@ export
446446
prod!,
447447
prod,
448448
promote_shape,
449-
randcycle,
450-
randcycle!,
451-
randperm,
452-
randperm!,
453-
randsubseq!,
454-
randsubseq,
455449
range,
456450
reducedim,
457451
repmat,
@@ -723,7 +717,6 @@ export
723717
print_shortest,
724718
print_with_color,
725719
println,
726-
randstring,
727720
repeat,
728721
replace,
729722
replace!,
@@ -755,20 +748,6 @@ export
755748
@warn,
756749
@error,
757750

758-
# random numbers
759-
AbstractRNG,
760-
MersenneTwister,
761-
RandomDevice,
762-
rand!,
763-
rand,
764-
randn!,
765-
randn,
766-
randexp!,
767-
randexp,
768-
srand,
769-
bitrand,
770-
randjump,
771-
772751
# bigfloat & precision
773752
precision,
774753
rounding,
@@ -1117,6 +1096,10 @@ export
11171096
unsafe_store!,
11181097
unsafe_write,
11191098

1099+
# implemented in Random module
1100+
rand,
1101+
randn,
1102+
11201103
# Macros
11211104
# parser internal
11221105
@__FILE__,

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/sysimg.jl

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,31 @@ include("lock.jl")
311311
include("threads.jl")
312312
include("weakkeydict.jl")
313313

314+
# To limit dependency on rand functionality (implemented in the Random
315+
# module), Crand is used in file.jl, and could be used in error.jl
316+
# (but it breaks a test)
317+
"""
318+
Crand([T::Type])
319+
320+
Interface to the C `rand()` function. If `T` is provided, generate a value of type `T`
321+
by composing two calls to `Crand()`. `T` can be `UInt32` or `Float64`.
322+
"""
323+
Crand() = ccall(:rand, Cuint, ())
324+
# RAND_MAX at least 2^15-1 in theory, but we assume 2^16-1 (in practice, it's 2^31-1)
325+
Crand(::Type{UInt32}) = ((Crand() % UInt32) << 16) (Crand() % UInt32)
326+
Crand(::Type{Float64}) = Crand(UInt32) / 2^32
327+
328+
"""
329+
Csrand([seed])
330+
331+
Interface the the C `srand(seed)` function.
332+
"""
333+
Csrand(seed=floor(time())) = ccall(:srand, Cvoid, (Cuint,), seed)
334+
335+
# functions defined in Random
336+
function rand end
337+
function randn end
338+
314339
# I/O
315340
include("stream.jl")
316341
include("socket.jl")
@@ -373,12 +398,6 @@ include("irrationals.jl")
373398
include("mathconstants.jl")
374399
using .MathConstants: ℯ, π, pi
375400

376-
# random number generation
377-
include("random/dSFMT.jl")
378-
include("random/random.jl")
379-
using .Random
380-
import .Random: rand, rand!
381-
382401
# (s)printf macros
383402
include("printf.jl")
384403
# import .Printf
@@ -471,6 +490,8 @@ using .Docs, .Markdown
471490
isdefined(Core, :Inference) && Docs.loaddocs(Core.Inference.CoreDocs.DOCS)
472491

473492
function __init__()
493+
# for the few uses of Crand in Base:
494+
Csrand()
474495
# Base library init
475496
reinit_stdio()
476497
global_logger(root_module(:Logging).ConsoleLogger(STDERR))
@@ -494,27 +515,29 @@ Base.require(:Base64)
494515
Base.require(:CRC32c)
495516
Base.require(:Dates)
496517
Base.require(:DelimitedFiles)
518+
Base.require(:Distributed)
497519
Base.require(:FileWatching)
498-
Base.require(:Logging)
520+
Base.require(:Future)
499521
Base.require(:IterativeEigensolvers)
522+
Base.require(:Libdl)
523+
Base.require(:Logging)
500524
Base.require(:Mmap)
525+
Base.require(:Printf)
501526
Base.require(:Profile)
527+
Base.require(:Random)
502528
Base.require(:SharedArrays)
503529
Base.require(:SparseArrays)
504530
Base.require(:SuiteSparse)
505531
Base.require(:Test)
506532
Base.require(:Unicode)
507-
Base.require(:Distributed)
508-
Base.require(:Printf)
509-
Base.require(:Future)
510-
Base.require(:Libdl)
511533

512534
@eval Base begin
513535
@deprecate_binding Test root_module(:Test) true ", run `using Test` instead"
514536
@deprecate_binding Mmap root_module(:Mmap) true ", run `using Mmap` instead"
515537
@deprecate_binding Profile root_module(:Profile) true ", run `using Profile` instead"
516538
@deprecate_binding Dates root_module(:Dates) true ", run `using Dates` instead"
517539
@deprecate_binding Distributed root_module(:Distributed) true ", run `using Distributed` instead"
540+
@deprecate_binding Random root_module(:Random) true ", run `using Random` instead"
518541

519542
# PR #25249
520543
@deprecate_binding SparseArrays root_module(:SparseArrays) true ", run `using SparseArrays` instead"

doc/src/base/arrays.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ Base.similar(::AbstractArray)
3434
Base.similar(::Any, ::Tuple)
3535
Base.linspace
3636
Base.logspace
37-
Base.Random.randsubseq
38-
Base.Random.randsubseq!
3937
```
4038

4139
## Basic functions
@@ -164,16 +162,10 @@ Base.mapslices
164162
## Combinatorics
165163

166164
```@docs
167-
Base.Random.randperm
168-
Base.Random.randperm!
169165
Base.invperm
170166
Base.isperm
171167
Base.permute!(::Any, ::AbstractVector)
172168
Base.invpermute!
173-
Base.Random.randcycle
174-
Base.Random.randcycle!
175-
Base.Random.shuffle
176-
Base.Random.shuffle!
177169
Base.reverse
178170
Base.reverseind
179171
Base.reverse!

doc/src/base/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@
3434
* [Sparse Arrays](@ref)
3535
* [Iterative Eigensolvers](@ref lib-itereigen)
3636
* [Printf](@ref)
37+
* [Random Numbers](@ref)

doc/src/base/numbers.md

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -130,38 +130,3 @@ BigFloat(x::Union{Integer, AbstractFloat, String}, rounding::RoundingMode)
130130
Base.MPFR.BigFloat(x, prec::Int, rounding::RoundingMode)
131131
Base.MPFR.BigFloat(x::String)
132132
```
133-
134-
## Random Numbers
135-
136-
Random number generation in Julia uses the [Mersenne Twister library](http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/#dSFMT)
137-
via `MersenneTwister` objects. Julia has a global RNG, which is used by default. Other RNG types
138-
can be plugged in by inheriting the `AbstractRNG` type; they can then be used to have multiple
139-
streams of random numbers. Besides `MersenneTwister`, Julia also provides the `RandomDevice` RNG
140-
type, which is a wrapper over the OS provided entropy.
141-
142-
Most functions related to random generation accept an optional `AbstractRNG` as the first argument,
143-
`rng` , which defaults to the global one if not provided. Morever, some of them accept optionally
144-
dimension specifications `dims...` (which can be given as a tuple) to generate arrays of random
145-
values.
146-
147-
A `MersenneTwister` or `RandomDevice` RNG can generate random numbers of the following types:
148-
[`Float16`](@ref), [`Float32`](@ref), [`Float64`](@ref), [`BigFloat`](@ref), [`Bool`](@ref),
149-
[`Int8`](@ref), [`UInt8`](@ref), [`Int16`](@ref), [`UInt16`](@ref), [`Int32`](@ref),
150-
[`UInt32`](@ref), [`Int64`](@ref), [`UInt64`](@ref), [`Int128`](@ref), [`UInt128`](@ref),
151-
[`BigInt`](@ref) (or complex numbers of those types).
152-
Random floating point numbers are generated uniformly in ``[0, 1)``. As `BigInt` represents
153-
unbounded integers, the interval must be specified (e.g. `rand(big(1:6))`).
154-
155-
```@docs
156-
Base.Random.srand
157-
Base.Random.MersenneTwister
158-
Base.Random.RandomDevice
159-
Base.Random.rand
160-
Base.Random.rand!
161-
Base.Random.bitrand
162-
Base.Random.randn
163-
Base.Random.randn!
164-
Base.Random.randexp
165-
Base.Random.randexp!
166-
Base.Random.randjump
167-
```

doc/src/base/strings.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ Base.chomp
5959
Base.thisind
6060
Base.nextind
6161
Base.prevind
62-
Base.Random.randstring
6362
Base.textwidth
6463
Base.isalpha
6564
Base.isascii

0 commit comments

Comments
 (0)