diff --git a/stdlib/Random/src/RNGs.jl b/stdlib/Random/src/RNGs.jl index deba1d34b9d97..0b929e1ebbf12 100644 --- a/stdlib/Random/src/RNGs.jl +++ b/stdlib/Random/src/RNGs.jl @@ -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()) diff --git a/stdlib/Random/src/Random.jl b/stdlib/Random/src/Random.jl index 61cca042e2e3e..4efa71c129720 100644 --- a/stdlib/Random/src/Random.jl +++ b/stdlib/Random/src/Random.jl @@ -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")) diff --git a/stdlib/Random/test/runtests.jl b/stdlib/Random/test/runtests.jl index be9e362c994cd..50ed155100b52 100644 --- a/stdlib/Random/test/runtests.jl +++ b/stdlib/Random/test/runtests.jl @@ -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