forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandom.jl
465 lines (331 loc) · 14.3 KB
/
Random.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# This file is a part of Julia. License is MIT: https://julialang.org/license
"""
Random
Support for generating random numbers. Provides [`rand`](@ref), [`randn`](@ref),
[`AbstractRNG`](@ref), [`Xoshiro`](@ref), [`MersenneTwister`](@ref), and [`RandomDevice`](@ref).
"""
module Random
include("DSFMT.jl")
using .DSFMT
using Base.GMP.MPZ
using Base.GMP: Limb
import SHA
using Base: BitInteger, BitInteger_types, BitUnsigned, require_one_based_indexing
import Base: copymutable, copy, copy!, ==, hash, convert,
rand, randn, show
export rand!, randn!,
randexp, randexp!,
bitrand,
randstring,
randsubseq, randsubseq!,
shuffle, shuffle!,
randperm, randperm!,
randcycle, randcycle!,
AbstractRNG, MersenneTwister, RandomDevice, TaskLocalRNG, Xoshiro
public seed!, default_rng, Sampler, SamplerType, SamplerTrivial, SamplerSimple
## general definitions
"""
AbstractRNG
Supertype for random number generators such as [`MersenneTwister`](@ref) and [`RandomDevice`](@ref).
"""
abstract type AbstractRNG end
Base.broadcastable(x::AbstractRNG) = Ref(x)
gentype(::Type{X}) where {X} = eltype(X)
gentype(x) = gentype(typeof(x))
### integers
# we define types which encode the generation of a specific number of bits
# the "raw" version means that the unused bits are not zeroed
abstract type UniformBits{T<:BitInteger} end
struct UInt10{T} <: UniformBits{T} end
struct UInt10Raw{T} <: UniformBits{T} end
struct UInt23{T} <: UniformBits{T} end
struct UInt23Raw{T} <: UniformBits{T} end
struct UInt52{T} <: UniformBits{T} end
struct UInt52Raw{T} <: UniformBits{T} end
struct UInt104{T} <: UniformBits{T} end
struct UInt104Raw{T} <: UniformBits{T} end
struct UInt2x52{T} <: UniformBits{T} end
struct UInt2x52Raw{T} <: UniformBits{T} end
uint_sup(::Type{<:Union{UInt10,UInt10Raw}}) = UInt16
uint_sup(::Type{<:Union{UInt23,UInt23Raw}}) = UInt32
uint_sup(::Type{<:Union{UInt52,UInt52Raw}}) = UInt64
uint_sup(::Type{<:Union{UInt104,UInt104Raw}}) = UInt128
uint_sup(::Type{<:Union{UInt2x52,UInt2x52Raw}}) = UInt128
for UI = (:UInt10, :UInt10Raw, :UInt23, :UInt23Raw, :UInt52, :UInt52Raw,
:UInt104, :UInt104Raw, :UInt2x52, :UInt2x52Raw)
@eval begin
$UI(::Type{T}=uint_sup($UI)) where {T} = $UI{T}()
# useful for defining rand generically:
uint_default(::$UI) = $UI{uint_sup($UI)}()
end
end
gentype(::Type{<:UniformBits{T}}) where {T} = T
### floats
abstract type FloatInterval{T<:AbstractFloat} end
struct CloseOpen01{T<:AbstractFloat} <: FloatInterval{T} end # interval [0,1)
struct CloseOpen12{T<:AbstractFloat} <: FloatInterval{T} end # interval [1,2)
const FloatInterval_64 = FloatInterval{Float64}
const CloseOpen01_64 = CloseOpen01{Float64}
const CloseOpen12_64 = CloseOpen12{Float64}
CloseOpen01(::Type{T}=Float64) where {T<:AbstractFloat} = CloseOpen01{T}()
CloseOpen12(::Type{T}=Float64) where {T<:AbstractFloat} = CloseOpen12{T}()
gentype(::Type{<:FloatInterval{T}}) where {T<:AbstractFloat} = T
const BitFloatType = Union{Type{Float16},Type{Float32},Type{Float64}}
### Sampler
abstract type Sampler{E} end
gentype(::Type{<:Sampler{E}}) where {E} = E
# temporarily for BaseBenchmarks
RangeGenerator(x) = Sampler(default_rng(), x)
# In some cases, when only 1 random value is to be generated,
# the optimal sampler can be different than if multiple values
# have to be generated. Hence a `Repetition` parameter is used
# to choose the best one depending on the need.
const Repetition = Union{Val{1},Val{Inf}}
# these default fall-back for all RNGs would be nice,
# but generate difficult-to-solve ambiguities
# Sampler(::AbstractRNG, X, ::Val{Inf}) = Sampler(X)
# Sampler(::AbstractRNG, ::Type{X}, ::Val{Inf}) where {X} = Sampler(X)
"""
Sampler(rng, x, repetition = Val(Inf))
Return a sampler object that can be used to generate random values from `rng` for `x`.
When `sp = Sampler(rng, x, repetition)`, `rand(rng, sp)` will be used to draw random values,
and should be defined accordingly.
`repetition` can be `Val(1)` or `Val(Inf)`, and should be used as a suggestion for deciding
the amount of precomputation, if applicable.
[`Random.SamplerType`](@ref) and [`Random.SamplerTrivial`](@ref) are default fallbacks for
*types* and *values*, respectively. [`Random.SamplerSimple`](@ref) can be used to store
pre-computed values without defining extra types for only this purpose.
"""
Sampler(rng::AbstractRNG, x, r::Repetition=Val(Inf)) = Sampler(typeof(rng), x, r)
Sampler(rng::AbstractRNG, ::Type{X}, r::Repetition=Val(Inf)) where {X} =
Sampler(typeof(rng), X, r)
# this method is necessary to prevent rand(rng::AbstractRNG, X) from
# recursively constructing nested Sampler types.
Sampler(T::Type{<:AbstractRNG}, sp::Sampler, r::Repetition) =
throw(MethodError(Sampler, (T, sp, r)))
# default shortcut for the general case
Sampler(::Type{RNG}, X) where {RNG<:AbstractRNG} = Sampler(RNG, X, Val(Inf))
Sampler(::Type{RNG}, ::Type{X}) where {RNG<:AbstractRNG,X} = Sampler(RNG, X, Val(Inf))
#### pre-defined useful Sampler types
"""
SamplerType{T}()
A sampler for types, containing no other information. The default fallback for `Sampler`
when called with types.
"""
struct SamplerType{T} <: Sampler{T} end
Sampler(::Type{<:AbstractRNG}, ::Type{T}, ::Repetition) where {T} = SamplerType{T}()
Base.getindex(::SamplerType{T}) where {T} = T
struct SamplerTrivial{T,E} <: Sampler{E}
self::T
end
"""
SamplerTrivial(x)
Create a sampler that just wraps the given value `x`. This is the default fall-back for
values.
The `eltype` of this sampler is equal to `eltype(x)`.
The recommended use case is sampling from values without precomputed data.
"""
SamplerTrivial(x::T) where {T} = SamplerTrivial{T,gentype(T)}(x)
Sampler(::Type{<:AbstractRNG}, x, ::Repetition) = SamplerTrivial(x)
Base.getindex(sp::SamplerTrivial) = sp.self
# simple sampler carrying data (which can be anything)
struct SamplerSimple{T,S,E} <: Sampler{E}
self::T
data::S
end
"""
SamplerSimple(x, data)
Create a sampler that wraps the given value `x` and the `data`.
The `eltype` of this sampler is equal to `eltype(x)`.
The recommended use case is sampling from values with precomputed data.
"""
SamplerSimple(x::T, data::S) where {T,S} = SamplerSimple{T,S,gentype(T)}(x, data)
Base.getindex(sp::SamplerSimple) = sp.self
# simple sampler carrying a (type) tag T and data
struct SamplerTag{T,S,E} <: Sampler{E}
data::S
SamplerTag{T}(s::S) where {T,S} = new{T,S,gentype(T)}(s)
end
#### helper samplers
# TODO: make constraining constructors to enforce that those
# types are <: Sampler{T}
##### Adapter to generate a random value in [0, n]
struct LessThan{T<:Integer,S} <: Sampler{T}
sup::T
s::S # the scalar specification/sampler to feed to rand
end
function rand(rng::AbstractRNG, sp::LessThan)
while true
x = rand(rng, sp.s)
x <= sp.sup && return x
end
end
struct Masked{T<:Integer,S} <: Sampler{T}
mask::T
s::S
end
rand(rng::AbstractRNG, sp::Masked) = rand(rng, sp.s) & sp.mask
##### Uniform
struct UniformT{T} <: Sampler{T} end
uniform(::Type{T}) where {T} = UniformT{T}()
rand(rng::AbstractRNG, ::UniformT{T}) where {T} = rand(rng, T)
### machinery for generation with Sampler
# This describes how to generate random scalars or arrays, by generating a Sampler
# and calling rand on it (which should be defined in "generation.jl").
# NOTE: this section could be moved into a separate file when more containers are supported.
#### scalars
rand(rng::AbstractRNG, X) = rand(rng, Sampler(rng, X, Val(1)))
# this is needed to disambiguate
rand(rng::AbstractRNG, X::Dims) = rand(rng, Sampler(rng, X, Val(1)))
rand(rng::AbstractRNG=default_rng(), ::Type{X}=Float64) where {X} = rand(rng, Sampler(rng, X, Val(1)))::X
rand(X) = rand(default_rng(), X)
rand(::Type{X}) where {X} = rand(default_rng(), X)
#### arrays
rand!(A::AbstractArray{T}, X) where {T} = rand!(default_rng(), A, X)
rand!(A::AbstractArray{T}, ::Type{X}=T) where {T,X} = rand!(default_rng(), A, X)
rand!(rng::AbstractRNG, A::AbstractArray{T}, X) where {T} = rand!(rng, A, Sampler(rng, X))
rand!(rng::AbstractRNG, A::AbstractArray{T}, ::Type{X}=T) where {T,X} = rand!(rng, A, Sampler(rng, X))
function rand!(rng::AbstractRNG, A::AbstractArray{T}, sp::Sampler) where T
for i in eachindex(A)
@inbounds A[i] = rand(rng, sp)
end
A
end
rand(r::AbstractRNG, dims::Integer...) = rand(r, Float64, Dims(dims))
rand( dims::Integer...) = rand(Float64, Dims(dims))
rand(r::AbstractRNG, X, dims::Dims) = rand!(r, Array{gentype(X)}(undef, dims), X)
rand( X, dims::Dims) = rand(default_rng(), X, dims)
rand(r::AbstractRNG, X, d::Integer, dims::Integer...) = rand(r, X, Dims((d, dims...)))
rand( X, d::Integer, dims::Integer...) = rand(X, Dims((d, dims...)))
# note: the above methods would trigger an ambiguity warning if d was not separated out:
# rand(r, ()) would match both this method and rand(r, dims::Dims)
# moreover, a call like rand(r, NotImplementedType()) would be an infinite loop
rand(r::AbstractRNG, ::Type{X}, dims::Dims) where {X} = rand!(r, Array{X}(undef, dims), X)
rand( ::Type{X}, dims::Dims) where {X} = rand(default_rng(), X, dims)
rand(r::AbstractRNG, ::Type{X}, d::Integer, dims::Integer...) where {X} = rand(r, X, Dims((d, dims...)))
rand( ::Type{X}, d::Integer, dims::Integer...) where {X} = rand(X, Dims((d, dims...)))
# SamplerUnion(X, Y, ...}) == Union{SamplerType{X}, SamplerType{Y}, ...}
SamplerUnion(U...) = Union{Any[SamplerType{T} for T in U]...}
const SamplerBoolBitInteger = SamplerUnion(Bool, BitInteger_types...)
include("Xoshiro.jl")
include("RNGs.jl")
include("generation.jl")
include("normal.jl")
include("misc.jl")
include("XoshiroSimd.jl")
## rand & rand! & seed! docstrings
"""
rand([rng=default_rng()], [S], [dims...])
Pick a random element or array of random elements from the set of values specified by `S`;
`S` can be
* an indexable collection (for example `1:9` or `('x', "y", :z)`)
* an `AbstractDict` or `AbstractSet` object
* a string (considered as a collection of characters), or
* a type from the list below, corresponding to the specified set of values
+ concrete integer types sample from `typemin(S):typemax(S)` (excepting [`BigInt`](@ref) which is not supported)
+ concrete floating point types sample from `[0, 1)`
+ concrete complex types `Complex{T}` if `T` is a sampleable type take their real and imaginary components
independently from the set of values corresponding to `T`, but are not supported if `T` is not sampleable.
+ all `<:AbstractChar` types sample from the set of valid Unicode scalars
+ a user-defined type and set of values; for implementation guidance please see [Hooking into the `Random` API](@ref rand-api-hook)
+ a tuple type of known size and where each parameter of `S` is itself a sampleable type; return a value of type `S`.
Note that tuple types such as `Tuple{Vararg{T}}` (unknown size) and `Tuple{1:2}` (parameterized with a value) are not supported
+ a `Pair` type, e.g. `Pair{X, Y}` such that `rand` is defined for `X` and `Y`,
in which case random pairs are produced.
`S` defaults to [`Float64`](@ref).
When only one argument is passed besides the optional `rng` and is a `Tuple`, it is interpreted
as a collection of values (`S`) and not as `dims`.
See also [`randn`](@ref) for normally distributed numbers, and [`rand!`](@ref) and [`randn!`](@ref) for the in-place equivalents.
!!! compat "Julia 1.1"
Support for `S` as a tuple requires at least Julia 1.1.
!!! compat "Julia 1.11"
Support for `S` as a `Tuple` type requires at least Julia 1.11.
# Examples
```julia-repl
julia> rand(Int, 2)
2-element Array{Int64,1}:
1339893410598768192
1575814717733606317
julia> using Random
julia> rand(Xoshiro(0), Dict(1=>2, 3=>4))
3 => 4
julia> rand((2, 3))
3
julia> rand(Float64, (2, 3))
2×3 Array{Float64,2}:
0.999717 0.0143835 0.540787
0.696556 0.783855 0.938235
```
!!! note
The complexity of `rand(rng, s::Union{AbstractDict,AbstractSet})`
is linear in the length of `s`, unless an optimized method with
constant complexity is available, which is the case for `Dict`,
`Set` and dense `BitSet`s. For more than a few calls, use `rand(rng,
collect(s))` instead, or either `rand(rng, Dict(s))` or `rand(rng,
Set(s))` as appropriate.
"""
rand
"""
rand!([rng=default_rng()], A, [S=eltype(A)])
Populate the array `A` with random values. If `S` is specified
(`S` can be a type or a collection, cf. [`rand`](@ref) for details),
the values are picked randomly from `S`.
This is equivalent to `copyto!(A, rand(rng, S, size(A)))`
but without allocating a new array.
# Examples
```jldoctest
julia> rand!(Xoshiro(123), zeros(5))
5-element Vector{Float64}:
0.521213795535383
0.5868067574533484
0.8908786980927811
0.19090669902576285
0.5256623915420473
```
"""
rand!
"""
seed!([rng=default_rng()], seed) -> rng
seed!([rng=default_rng()]) -> rng
Reseed the random number generator: `rng` will give a reproducible
sequence of numbers if and only if a `seed` is provided. Some RNGs
don't accept a seed, like `RandomDevice`.
After the call to `seed!`, `rng` is equivalent to a newly created
object initialized with the same seed.
The types of accepted seeds depend on the type of `rng`, but in general,
integer seeds should work.
If `rng` is not specified, it defaults to seeding the state of the
shared task-local generator.
# Examples
```julia-repl
julia> Random.seed!(1234);
julia> x1 = rand(2)
2-element Vector{Float64}:
0.32597672886359486
0.5490511363155669
julia> Random.seed!(1234);
julia> x2 = rand(2)
2-element Vector{Float64}:
0.32597672886359486
0.5490511363155669
julia> x1 == x2
true
julia> rng = Xoshiro(1234); rand(rng, 2) == x1
true
julia> Xoshiro(1) == Random.seed!(rng, 1)
true
julia> rand(Random.seed!(rng), Bool) # not reproducible
true
julia> rand(Random.seed!(rng), Bool) # not reproducible either
false
julia> rand(Xoshiro(), Bool) # not reproducible either
true
```
"""
seed!(rng::AbstractRNG) = seed!(rng, nothing)
#=
We have this generic definition instead of the alternative option
`seed!(rng::AbstractRNG, ::Nothing) = seed!(rng)`
because it would lead too easily to ambiguities, e.g. when we define `seed!(::Xoshiro, seed)`.
=#
end # module