Skip to content

Commit b8a66bd

Browse files
authored
Use Random.default_rng() instead of Random.GLOBAL_RNG
1 parent 878efce commit b8a66bd

File tree

7 files changed

+12
-12
lines changed

7 files changed

+12
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ function sample(
289289
Draw `n_samples` samples using the kernel `κ` under the Hamiltonian system `h`
290290

291291
- The randomness is controlled by `rng`.
292-
- If `rng` is not provided, `GLOBAL_RNG` will be used.
292+
- If `rng` is not provided, the default random number generator (`Random.default_rng()`) will be used.
293293
- The initial point is given by `θ`.
294294
- The adaptor is set by `adaptor`, for which the default is no adaptation.
295295
- It will perform `n_adapts` steps of adaptation, for which the default is `1_000` or 10% of `n_samples`, whichever is lower.

research/src/riemannian_hmc.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ Base.rand(rng::AbstractRNG, metric::AbstractMetric, kinetic, θ) =
145145
_rand(rng, metric, kinetic) # this disambiguity is required by Random.rand
146146
Base.rand(rng::AbstractVector{<:AbstractRNG}, metric::AbstractMetric, kinetic, θ) =
147147
_rand(rng, metric, kinetic)
148-
Base.rand(metric::AbstractMetric, kinetic, θ) = rand(GLOBAL_RNG, metric, kinetic)
148+
Base.rand(metric::AbstractMetric, kinetic, θ) = rand(Random.default_rng(), metric, kinetic)
149149

150150
### metric.jl
151151

src/AdvancedHMC.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ using LinearAlgebra:
77
Symmetric, UpperTriangular, mul!, ldiv!, dot, I, diag, cholesky, UniformScaling
88
using StatsFuns: logaddexp, logsumexp
99
import Random
10-
using Random: GLOBAL_RNG, AbstractRNG
10+
using Random: AbstractRNG
1111
using ProgressMeter: ProgressMeter
1212
using SimpleUnPack: @unpack
1313

src/metric.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ Base.rand(
132132
kinetic::AbstractKinetic,
133133
) = _rand(rng, metric, kinetic)
134134
Base.rand(metric::AbstractMetric, kinetic::AbstractKinetic) =
135-
rand(GLOBAL_RNG, metric, kinetic)
135+
rand(Random.default_rng(), metric, kinetic)
136136

137137
# ignore θ by default unless defined by the specific kinetic (i.e. not position-dependent)
138138
Base.rand(

src/sampler.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ sample(
118118
progress::Bool = false,
119119
(pm_next!)::Function = pm_next!,
120120
) = sample(
121-
GLOBAL_RNG,
121+
Random.default_rng(),
122122
h,
123123
κ,
124124
θ,
@@ -146,7 +146,7 @@ sample(
146146
)
147147
Sample `n_samples` samples using the proposal `κ` under Hamiltonian `h`.
148148
- The randomness is controlled by `rng`.
149-
- If `rng` is not provided, `GLOBAL_RNG` will be used.
149+
- If `rng` is not provided, the default random number generator (`Random.default_rng()`) will be used.
150150
- The initial point is given by `θ`.
151151
- The adaptor is set by `adaptor`, for which the default is no adaptation.
152152
- It will perform `n_adapts` steps of adaptation, for which the default is the minimum of `1_000` and 10% of `n_samples`

src/trajectory.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
####
44
#### Developers' Notes
55
####
6-
#### Not all functions that use `rng` require a fallback function with `GLOBAL_RNG`
6+
#### Not all functions that use `rng` require a fallback function with `Random.default_rng()`
77
#### as default. In short, only those exported to other libries need such a fallback
88
#### function. Internal uses shall always use the explict `rng` version. (Kai Xu 6/Jul/19)
99

@@ -241,10 +241,10 @@ $(SIGNATURES)
241241
242242
Make a MCMC transition from phase point `z` using the trajectory `τ` under Hamiltonian `h`.
243243
244-
NOTE: This is a RNG-implicit fallback function for `transition(GLOBAL_RNG, τ, h, z)`
244+
NOTE: This is a RNG-implicit fallback function for `transition(Random.default_rng(), τ, h, z)`
245245
"""
246246
function transition::Trajectory, h::Hamiltonian, z::PhasePoint)
247-
return transition(GLOBAL_RNG, τ, h, z)
247+
return transition(Random.default_rng(), τ, h, z)
248248
end
249249

250250
###
@@ -834,7 +834,7 @@ function find_good_stepsize(
834834
θ::AbstractVector{<:AbstractFloat};
835835
max_n_iters::Int = 100,
836836
)
837-
return find_good_stepsize(GLOBAL_RNG, h, θ; max_n_iters = max_n_iters)
837+
return find_good_stepsize(Random.default_rng(), h, θ; max_n_iters = max_n_iters)
838838
end
839839

840840
"Perform MH acceptance based on energy, i.e. negative log probability."

test/integrator.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ using Statistics: mean
3939
@test AdvancedHMC.nom_step_size(lf) == ϵ0
4040
@test AdvancedHMC.step_size(lf) == ϵ0
4141

42-
lf2 = AdvancedHMC.jitter(Random.GLOBAL_RNG, lf)
42+
lf2 = AdvancedHMC.jitter(Random.default_rng(), lf)
4343
@test lf2 === lf
4444
@test AdvancedHMC.nom_step_size(lf2) == ϵ0
4545
@test AdvancedHMC.step_size(lf2) == ϵ0
@@ -53,7 +53,7 @@ using Statistics: mean
5353
@test lf.ϵ == ϵ0
5454
@test AdvancedHMC.step_size(lf) == lf.ϵ
5555

56-
lf2 = AdvancedHMC.jitter(Random.GLOBAL_RNG, lf)
56+
lf2 = AdvancedHMC.jitter(Random.default_rng(), lf)
5757
@test lf2.ϵ0 == ϵ0
5858
@test AdvancedHMC.nom_step_size(lf2) == ϵ0
5959
@test lf2.ϵ != ϵ0

0 commit comments

Comments
 (0)