Skip to content

Commit a3b8f6d

Browse files
slwu89cscherrer
andauthored
add method for Distributions.rand(::ResettableRNG, ::Binomial) (#249)
* add method for Distributions.rand(::ResettableRNG, ::Binomial * Fix ResettableRNG dispatch * Test with MersenneTwister (Xoshiro not available in Julia 1.6) * bump version --------- Co-authored-by: Chad Scherrer <[email protected]>
1 parent 9960e9b commit a3b8f6d

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "MeasureTheory"
22
uuid = "eadaa1a4-d27c-401d-8699-e962e1bbc33b"
33
authors = ["Chad Scherrer <[email protected]> and contributors"]
4-
version = "0.18.1"
4+
version = "0.18.2"
55

66
[deps]
77
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"

src/parameterized/binomial.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ end
2323
x (0, 1)
2424
end
2525

26-
function Base.rand(rng::AbstractRNG, ::Type, d::Binomial{(:n, :p)})
27-
rand(rng, Dists.Binomial(d.n, d.p))
26+
function Base.rand(rng::AbstractRNG, ::Type{T}, d::Binomial{(:n, :p)}) where {T}
27+
rand(rng, T, Dists.Binomial(d.n, d.p))
2828
end
2929

3030
Binomial(n) = Binomial(n, 0.5)

src/resettable-rng.jl

+8
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ for T in vcat(subtypes(Signed), subtypes(Unsigned), subtypes(AbstractFloat))
5757
end
5858
end
5959

60+
function Base.rand(r::ResettableRNG, d::AbstractMeasure)
61+
rand(r.rng, d)
62+
end
63+
64+
function Base.rand(r::ResettableRNG, ::Type{T}, d::AbstractMeasure) where {T}
65+
rand(r.rng, T, d)
66+
end
67+
6068
Base.iterate(r::ResettableRNG) = iterate(r, nothing)
6169

6270
function Base.iterate(r::ResettableRNG, ::Nothing)

test/runtests.jl

+4
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ end
116116
@test logdensity_def(Binomial(; n, logitp), y)
117117
@test logdensity_def(Binomial(; n, probitp), y)
118118

119+
rng = ResettableRNG(Random.MersenneTwister())
120+
@test rand(rng, Binomial(n=0, p=1.0)) == 0
121+
@test rand(rng, Binomial(n=10, p=1.0)) == 10
122+
119123
@test_broken logdensity_def(Binomial(n, p), CountingMeasure(ℤ[0:n]), x)
120124
binomlogpdf(n, p, x)
121125
end

0 commit comments

Comments
 (0)