# Using Random.SamplerSimple

**URL:** <https://discourse.julialang.org/t/using-random-samplersimple/17864>\
**Category:** Internals & Design\
**Tags:** question\
**Created:** [November 22, 2018, 2:04pm UTC](https://discourse.julialang.org/t/using-random-samplersimple/17864 "2018-11-22T14:04:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 22, 2018, 2:04pm UTC](https://discourse.julialang.org/t/using-random-samplersimple/17864/1 "2018-11-22T14:04:27Z")

</div>

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](https://docs.julialang.org/en/v1/stdlib/Random/) (which I found hard to comprehend, but reading the source helped).

For the first one,

```julia
# 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

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

```

---

<div class="post-metadata">

**Author:** ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)\
**Post date:** [November 23, 2018, 6:39pm UTC](https://discourse.julialang.org/t/using-random-samplersimple/17864/2 "2018-11-23T18:39:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 23, 2018, 6:50pm UTC](https://discourse.julialang.org/t/using-random-samplersimple/17864/3 "2018-11-23T18:50:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)\
**Post date:** [December 8, 2018, 11:35am UTC](https://discourse.julialang.org/t/using-random-samplersimple/17864/4 "2018-12-08T11:35:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 8, 2018, 11:45am UTC](https://discourse.julialang.org/t/using-random-samplersimple/17864/5 "2018-12-08T11:45:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 21, 2019, 12:24pm UTC](https://discourse.julialang.org/t/using-random-samplersimple/17864/6 "2019-04-21T12:24:59Z")

</div>

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

[https://github.com/JuliaLang/julia/pull/31787](https://github.com/JuliaLang/julia/pull/31787)

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