I am trying to come up with MWEs for objects with and without precomputed data for generating draws. If someone could check that this is the way to go, I would contribute to the manual (which I found hard to comprehend, but reading the source helped).
For the first one,
# no precomputed sampler
import Random
struct Foo end # Bernoulli w/p 0.5
Random.rand(rng::Random.AbstractRNG,
sampler::Random.SamplerTrivial{Foo}) = rand(rng, Float64) < 0.5
works fine.
Now suppose that I want to precompute something, eg
struct Bar{T} # Bernoulli w/p √threshold
threshold::T
end
Random.Sampler(::Type{<:Random.AbstractRNG}, bar::Bar, ::Random.Repetition) =
Random.SamplerSimple(bar, √bar.threshold)
Random.rand(rng::Random.AbstractRNG, sampler::Random.SamplerSimple{<:Bar}) =
rand(rng, Float64) < sampler.data