Skip to content

Commit

Permalink
RNG: fix Sampler selection for GLOBAL_RNG (#33172)
Browse files Browse the repository at this point in the history
  • Loading branch information
rfourquet authored and KristofferC committed Sep 6, 2019
1 parent 36c59c1 commit 4c8cd3b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
3 changes: 3 additions & 0 deletions stdlib/Random/src/RNGs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,9 @@ struct _GLOBAL_RNG <: AbstractRNG
global const GLOBAL_RNG = _GLOBAL_RNG.instance
end

# GLOBAL_RNG currently represents a MersenneTwister
typeof_rng(::_GLOBAL_RNG) = MersenneTwister

copy!(dst::MersenneTwister, ::_GLOBAL_RNG) = copy!(dst, default_rng())
copy!(::_GLOBAL_RNG, src::MersenneTwister) = copy!(default_rng(), src)
copy(::_GLOBAL_RNG) = copy(default_rng())
Expand Down
7 changes: 5 additions & 2 deletions stdlib/Random/src/Random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,11 @@ the amount of precomputation, if applicable.
*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)
Sampler(rng::AbstractRNG, x, r::Repetition=Val(Inf)) = Sampler(typeof_rng(rng), x, r)
Sampler(rng::AbstractRNG, ::Type{X}, r::Repetition=Val(Inf)) where {X} =
Sampler(typeof_rng(rng), X, r)

typeof_rng(rng::AbstractRNG) = typeof(rng)

Sampler(::Type{<:AbstractRNG}, sp::Sampler, ::Repetition) =
throw(ArgumentError("Sampler for this object is not defined"))
Expand Down
3 changes: 3 additions & 0 deletions stdlib/Random/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -770,4 +770,7 @@ end
B = fill!(B, 1.0)
@test rand!(GLOBAL_RNG, A, x) === A == rand!(mt, B, x) === B
end
# issue #33170
@test Sampler(GLOBAL_RNG, 2:4, Val(1)) isa SamplerRangeFast
@test Sampler(GLOBAL_RNG, 2:4, Val(Inf)) isa SamplerRangeFast
end

0 comments on commit 4c8cd3b

Please sign in to comment.