Skip to content

Commit d82601b

Browse files
committed
eliminate implicit usage of GLOBAL_RNG
1 parent 72e641d commit d82601b

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/AlphaStableDistributions.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ Base.@kwdef struct SymmetricAlphaStable{T} <: Distributions.ContinuousUnivariate
236236
end
237237
Distributions.params(d::SymmetricAlphaStable) = (d.α, d.scale, d.location)
238238
Distributions.cf(d::SymmetricAlphaStable, t::Real) = cf(AlphaStable(d), t)
239+
Random.rand(rng::AbstractRNG, d::SymmetricAlphaStable) = rand(rng, AlphaStable(d))
239240

240241
function AlphaStable(d::SymmetricAlphaStable)
241242
AlphaStable=d.α,scale=d.scale,location=d.location)
@@ -412,9 +413,9 @@ function Random.rand!(rng::AbstractRNG, d::AlphaSubGaussian{T}, x::AbstractArray
412413
nmax, nmin, res, rind, vjoint = matdict["Nmax"]::Float64, matdict["Nmin"]::Float64, matdict["res"]::Float64, vec(matdict["rind"])::Vector{Float64}, matdict["vJoint"]::Matrix{Float64}
413414
step = (log10(nmax)-log10(nmin))/res
414415
m>size(vjoint, 1)-1 && throw(DomainError(R, "The dimensions of `R` exceed the maximum possible 10x10"))
415-
A = rand(AlphaStable(T/2), one(T), T(2*cos*α/4)^(2.0/α)), zero(T)))
416-
CT = rand(Chisq(m))
417-
S = randn(m)
416+
A = rand(rng,AlphaStable(T/2), one(T), T(2*cos*α/4)^(2.0/α)), zero(T)))
417+
CT = rand(rng,Chisq(m))
418+
S = randn(rng,m)
418419
S = S/sqrt(sum(abs2,S))
419420
xtmp = ((sigrootx1*sqrt(A*CT))*S)'
420421
if n<=m
@@ -430,8 +431,8 @@ function Random.rand!(rng::AbstractRNG, d::AlphaSubGaussian{T}, x::AbstractArray
430431
norm1 = subgausscondprobtabulate(α, x1, mode, invRx1, invR, vjoint, nmin, nmax, step, rind, kappa, k1, k2, kmarg)
431432
notaccept = true
432433
while notaccept
433-
u = rand()
434-
v = (norms/norm1)*rand(TDist(vstud)) + mode
434+
u = rand(rng)
435+
v = (norms/norm1)*rand(rng,TDist(vstud)) + mode
435436
gv = (norm1/norms)*pdf(TDist(vstud), (v-mode)*(norm1/norms))
436437
fv = subgausscondprobtabulate(α, x1, v, invRx1, invR, vjoint, nmin, nmax, step, rind, kappa, k1, k2, kmarg)
437438
if c*u <= fv/gv

test/runtests.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
using AlphaStableDistributions
22
using Test, Random, Distributions
33

4+
@testset "Reproducibility" begin
5+
@test rand(MersenneTwister(0), AlphaStable() ) == rand(MersenneTwister(0), AlphaStable() )
6+
@test rand(MersenneTwister(0), SymmetricAlphaStable()) == rand(MersenneTwister(0), SymmetricAlphaStable())
7+
@test rand(MersenneTwister(0), AlphaSubGaussian(n=10)) == rand(MersenneTwister(0), AlphaSubGaussian(n=10))
8+
end
9+
410
@testset "cf" begin
511
rng = MersenneTwister(1)
612
for _ in 1:100
@@ -122,8 +128,8 @@ end
122128
d5 = fit(AlphaSubGaussian, x, m, p=1.0)
123129
@test d4.α d5.α rtol=0.1
124130
@test d4.R d5.R rtol=0.1
125-
126131
end
132+
127133
# 362.499 ms (4620903 allocations: 227.64 MiB)
128134
# 346.520 ms (4621052 allocations: 209.62 MiB) # StaticArrays in outer fun
129135
# 345.925 ms (4225524 allocations: 167.66 MiB) # tempind to tuple

0 commit comments

Comments
 (0)