Skip to content

Commit 4c8cd3b

Browse files
rfourquetKristofferC
authored andcommitted
RNG: fix Sampler selection for GLOBAL_RNG (#33172)
1 parent 36c59c1 commit 4c8cd3b

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

stdlib/Random/src/RNGs.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,9 @@ struct _GLOBAL_RNG <: AbstractRNG
313313
global const GLOBAL_RNG = _GLOBAL_RNG.instance
314314
end
315315

316+
# GLOBAL_RNG currently represents a MersenneTwister
317+
typeof_rng(::_GLOBAL_RNG) = MersenneTwister
318+
316319
copy!(dst::MersenneTwister, ::_GLOBAL_RNG) = copy!(dst, default_rng())
317320
copy!(::_GLOBAL_RNG, src::MersenneTwister) = copy!(default_rng(), src)
318321
copy(::_GLOBAL_RNG) = copy(default_rng())

stdlib/Random/src/Random.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,11 @@ the amount of precomputation, if applicable.
136136
*types* and *values*, respectively. [`Random.SamplerSimple`](@ref) can be used to store
137137
pre-computed values without defining extra types for only this purpose.
138138
"""
139-
Sampler(rng::AbstractRNG, x, r::Repetition=Val(Inf)) = Sampler(typeof(rng), x, r)
140-
Sampler(rng::AbstractRNG, ::Type{X}, r::Repetition=Val(Inf)) where {X} = Sampler(typeof(rng), X, r)
139+
Sampler(rng::AbstractRNG, x, r::Repetition=Val(Inf)) = Sampler(typeof_rng(rng), x, r)
140+
Sampler(rng::AbstractRNG, ::Type{X}, r::Repetition=Val(Inf)) where {X} =
141+
Sampler(typeof_rng(rng), X, r)
142+
143+
typeof_rng(rng::AbstractRNG) = typeof(rng)
141144

142145
Sampler(::Type{<:AbstractRNG}, sp::Sampler, ::Repetition) =
143146
throw(ArgumentError("Sampler for this object is not defined"))

stdlib/Random/test/runtests.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,4 +770,7 @@ end
770770
B = fill!(B, 1.0)
771771
@test rand!(GLOBAL_RNG, A, x) === A == rand!(mt, B, x) === B
772772
end
773+
# issue #33170
774+
@test Sampler(GLOBAL_RNG, 2:4, Val(1)) isa SamplerRangeFast
775+
@test Sampler(GLOBAL_RNG, 2:4, Val(Inf)) isa SamplerRangeFast
773776
end

0 commit comments

Comments
 (0)