Using Random.SamplerSimple

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
            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

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) <

I don’t have access to a computer now and didn’t recheck the docs, but yes it looks like the way to go. Did you finish the last sentence in the OP?
It would be amazing if you can improve the docs! I will have more time next week for giving feed back.

Yes, the last example is meant to be a demo of precomputing a quantity.

I will wait for more feedback next week, then make a PR and we can continue the discussion there.

I can re-confirm that your example is implemented as expected by the API, I will be happy to answer any other questions!

Thanks, I will make a PR during the Xmas break.

1 Like

@rfourquet: A bit later than I expected to get to it, but I made

If you have the time for a review, it would be appreciated.

1 Like