Skip to content

Commit 0bd67f0

Browse files
committed
add Rand objects
1 parent 20030fd commit 0bd67f0

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

base/random/generation.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ rand_ui52(r::AbstractRNG) = rand_ui52_raw(r) & 0x000fffffffffffff
115115

116116
### sampler for pairs and complex numbers
117117

118-
function Sampler(rng::AbstractRNG, u::Distribution2{T}, n::Repetition) where T <: Union{Complex,Pair}
118+
function Sampler(rng::AbstractRNG, u::Combine2{T}, n::Repetition) where T <: Union{Complex,Pair}
119119
sp1 = Sampler(rng, u.x, n)
120120
sp2 = u.x == u.y ? sp1 : Sampler(rng, u.y, n)
121121
SamplerTag{Cont{T}}((sp1, sp2))
@@ -126,11 +126,11 @@ rand(rng::AbstractRNG, sp::SamplerTag{Cont{T}}) where {T<:Union{Complex,Pair}} =
126126

127127
#### additional methods for complex numbers
128128

129-
Sampler(rng::AbstractRNG, u::Distribution1{Complex}, n::Repetition) =
130-
Sampler(rng, Distribution(Complex, u.x, u.x), n)
129+
Sampler(rng::AbstractRNG, u::Combine1{Complex}, n::Repetition) =
130+
Sampler(rng, Combine(Complex, u.x, u.x), n)
131131

132132
Sampler(rng::AbstractRNG, ::Type{Complex{T}}, n::Repetition) where {T<:Real} =
133-
Sampler(rng, Distribution(Complex, T, T), n)
133+
Sampler(rng, Combine(Complex, T, T), n)
134134

135135
### random characters
136136

base/random/random.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,33 @@ rand(::Type{BitArray}, dims::Dims) = rand!(BitArray(uninitialized, dims))
351351
rand(::Type{BitArray}, dims::Integer...) = rand!(BitArray(uninitialized, convert(Dims, dims)))
352352

353353

354+
### Rand
355+
356+
struct Rand{R<:AbstractRNG,S<:Sampler}
357+
rng::R
358+
sp::S
359+
end
360+
361+
# X can be an explicit Distribution, or an implicit one like 1:10
362+
Rand(rng::AbstractRNG, X) = Rand(rng, Sampler(rng, X))
363+
Rand(rng::AbstractRNG, ::Type{X}) where {X} = Rand(rng, Sampler(rng, X))
364+
365+
Rand(X) = Rand(GLOBAL_RNG, X)
366+
Rand(::Type{X}) where {X} = Rand(GLOBAL_RNG, X)
367+
368+
(R::Rand)(args...) = rand(R.rng, R.sp, args...)
369+
370+
Base.start(R::Rand) = R
371+
372+
function Base.next(R::Rand, ::Rand)
373+
e = R()
374+
e, R
375+
end
376+
377+
Base.done(R::Rand, ::Rand) = false
378+
379+
Base.iteratorsize(::Type{<:Rand}) = Base.IsInfinite()
380+
354381
## __init__ & include
355382

356383
function __init__()

0 commit comments

Comments
 (0)