Skip to content

Commit 5af1bad

Browse files
committed
use faster default_rng() instead of GLOBAL_RNG
1 parent 51a48da commit 5af1bad

3 files changed

Lines changed: 28 additions & 22 deletions

File tree

src/RandomExtensions.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ using Random: GLOBAL_RNG, SamplerTrivial, SamplerSimple, SamplerTag, SamplerType
1313

1414
using SparseArrays: sprand, sprandn, AbstractSparseArray, SparseVector, SparseMatrixCSC
1515

16+
if isdefined(Random, :default_rng)
17+
using Random: default_rng
18+
else
19+
@inline default_rng() = GLOBAL_RNG
20+
end
21+
1622

1723
## a dummy container type to take advangage of SamplerTag constructor
1824

@@ -35,8 +41,8 @@ val_gentype(::Type{X}) where {X} = X
3541

3642
## rand! for non-containers
3743

38-
rand!(y, X) = rand!(GLOBAL_RNG, y, X)
39-
rand!(y, ::Type{X}) where {X} = rand!(GLOBAL_RNG, y, X)
44+
rand!(y, X) = rand!(default_rng(), y, X)
45+
rand!(y, ::Type{X}) where {X} = rand!(default_rng(), y, X)
4046

4147
rand!(rng::AbstractRNG, y, X) = rand!(rng, y, Sampler(rng, X, Val(1)))
4248
rand!(rng::AbstractRNG, y, ::Type{X}) where {X} = rand!(rng, y, Sampler(rng, X, Val(1)))

src/containers.jl

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ macro make_container(margs...)
5050
end
5151
end
5252
for def in (:(rand(rng::AbstractRNG) where {} = rand(rng, make())),
53-
:(rand() where {} = rand(GLOBAL_RNG, make())))
53+
:(rand() where {} = rand(default_rng(), make())))
5454
append!(def.args[1].args[1].args, filter(!=(()), as1))
5555
as2 = copy(as1)
5656
as2[1], as2[2] = as2[2], as2[1]
@@ -95,20 +95,20 @@ end
9595

9696
macro make_array_container(Cont)
9797
definitions =
98-
[ :(rand(rng::AbstractRNG, $Cont, dims::Dims) = rand(rng, _make_cont(t, dims))),
99-
:(rand( $Cont, dims::Dims) = rand(GLOBAL_RNG, _make_cont(t, dims))),
100-
:(rand(rng::AbstractRNG, $Cont, dims::Integer...) = rand(rng, _make_cont(t, Dims(dims)))),
101-
:(rand( $Cont, dims::Integer...) = rand(GLOBAL_RNG, _make_cont(t, Dims(dims)))),
102-
103-
:(rand(rng::AbstractRNG, X, $Cont, dims::Dims) = rand(rng, _make_cont(t, X, dims))),
104-
:(rand( X, $Cont, dims::Dims) = rand(GLOBAL_RNG, _make_cont(t, X, dims))),
105-
:(rand(rng::AbstractRNG, X, $Cont, dims::Integer...) = rand(rng, _make_cont(t, X, Dims(dims)))),
106-
:(rand( X, $Cont, dims::Integer...) = rand(GLOBAL_RNG, _make_cont(t, X, Dims(dims)))),
107-
108-
:(rand(rng::AbstractRNG, ::Type{X}, $Cont, dims::Dims) where {X} = rand(rng, _make_cont(t, X, dims))),
109-
:(rand( ::Type{X}, $Cont, dims::Dims) where {X} = rand(GLOBAL_RNG, _make_cont(t, X, dims))),
110-
:(rand(rng::AbstractRNG, ::Type{X}, $Cont, dims::Integer...) where {X} = rand(rng, _make_cont(t, X, Dims(dims)))),
111-
:(rand( ::Type{X}, $Cont, dims::Integer...) where {X} = rand(GLOBAL_RNG, _make_cont(t, X, Dims(dims)))),
98+
[ :(rand(rng::AbstractRNG, $Cont, dims::Dims) = rand(rng, _make_cont(t, dims))),
99+
:(rand( $Cont, dims::Dims) = rand(default_rng(), _make_cont(t, dims))),
100+
:(rand(rng::AbstractRNG, $Cont, dims::Integer...) = rand(rng, _make_cont(t, Dims(dims)))),
101+
:(rand( $Cont, dims::Integer...) = rand(default_rng(), _make_cont(t, Dims(dims)))),
102+
103+
:(rand(rng::AbstractRNG, X, $Cont, dims::Dims) = rand(rng, _make_cont(t, X, dims))),
104+
:(rand( X, $Cont, dims::Dims) = rand(default_rng(), _make_cont(t, X, dims))),
105+
:(rand(rng::AbstractRNG, X, $Cont, dims::Integer...) = rand(rng, _make_cont(t, X, Dims(dims)))),
106+
:(rand( X, $Cont, dims::Integer...) = rand(default_rng(), _make_cont(t, X, Dims(dims)))),
107+
108+
:(rand(rng::AbstractRNG, ::Type{X}, $Cont, dims::Dims) where {X} = rand(rng, _make_cont(t, X, dims))),
109+
:(rand( ::Type{X}, $Cont, dims::Dims) where {X} = rand(default_rng(), _make_cont(t, X, dims))),
110+
:(rand(rng::AbstractRNG, ::Type{X}, $Cont, dims::Integer...) where {X} = rand(rng, _make_cont(t, X, Dims(dims)))),
111+
:(rand( ::Type{X}, $Cont, dims::Integer...) where {X} = rand(default_rng(), _make_cont(t, X, Dims(dims)))),
112112
]
113113
esc(Expr(:block, definitions...))
114114
end
@@ -133,9 +133,9 @@ function _rand!(rng::AbstractRNG, A::SetDict, n::Integer, sp::Sampler)
133133
A
134134
end
135135

136-
rand!( A::SetDict, X=default_sampling(A)) = rand!(GLOBAL_RNG, A, X)
136+
rand!( A::SetDict, X=default_sampling(A)) = rand!(default_rng(), A, X)
137137
rand!(rng::AbstractRNG, A::SetDict, X=default_sampling(A)) = _rand!(rng, A, length(A), sampler(rng, X))
138-
rand!( A::SetDict, ::Type{X}) where {X} = rand!(GLOBAL_RNG, A, X)
138+
rand!( A::SetDict, ::Type{X}) where {X} = rand!(default_rng(), A, X)
139139
rand!(rng::AbstractRNG, A::SetDict, ::Type{X}) where {X} = rand!(rng, A, Sampler(rng, X))
140140

141141
@make_container(T::Type{<:SetDict}, n::Integer)

src/iteration.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ end
1111
Rand(rng::AbstractRNG, X) = Rand(rng, Sampler(rng, X))
1212
Rand(rng::AbstractRNG, ::Type{X}=Float64) where {X} = Rand(rng, Sampler(rng, X))
1313

14-
Rand(X) = Rand(GLOBAL_RNG, X)
15-
Rand(::Type{X}=Float64) where {X} = Rand(GLOBAL_RNG, X)
14+
Rand(X) = Rand(default_rng(), X)
15+
Rand(::Type{X}=Float64) where {X} = Rand(default_rng(), X)
1616

1717
(R::Rand)(args...) = rand(R.rng, R.sp, args...)
1818

@@ -24,5 +24,5 @@ Base.eltype(::Type{<:Rand{R, <:Sampler{T}}}) where {R,T} = T
2424

2525
# convenience iteration over distributions
2626

27-
Base.iterate(d::Distribution) = iterate(Rand(GLOBAL_RNG, d))
27+
Base.iterate(d::Distribution) = iterate(Rand(default_rng(), d))
2828
Base.IteratorSize(::Type{<:Distribution}) = Base.IsInfinite()

0 commit comments

Comments
 (0)