# How to define a Sampler for an object?

**URL:** https://discourse.julialang.org/t/how-to-define-a-sampler-for-an-object/37965
**Category:** General Usage
**Tags:** question
**Created:** [April 21, 2020, 2:01pm UTC](https://discourse.julialang.org/t/how-to-define-a-sampler-for-an-object/37965 "2020-04-21T14:01:46Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![logankilpatrick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/logankilpatrick/32/38123_2.png) [@logankilpatrick](https://discourse.julialang.org/u/logankilpatrick)
#### Post date: [April 21, 2020, 2:01pm UTC](https://discourse.julialang.org/t/how-to-define-a-sampler-for-an-object/37965/1 "2020-04-21T14:01:46Z")

</div>

Hey all,

I am getting an error when trying to use the `Rand` function for my custom type. The error is:

> ERROR: LoadError: ArgumentError: Sampler for this object is not defined

I read the docs [here](https://docs.julialang.org/en/v1/stdlib/Random/#Random.Sampler) and [here](https://docs.julialang.org/en/v1/stdlib/Random/#Custom-sampler-types-1) but it is not clear to me where I need to define the Sampler or what the syntax I should use to do so would be.

I cannot share a MWE as the code is private, unfortunately. Any suggestions on how set up the Sampler would be much appreciated!

Here is the `Rand` function:

```julia
function rand(rng::AbstractRNG, p::VDPTagPOMDP{CardinalBarriers})
    # p is just some arbitrary type used to dispatch correctly.
    return TagState([0.0, 0.0], 8.0*rand(rng, 2) .- 4.0)

end

struct TagState
    agent::Vec2
    target::Vec2
end

```

---

<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, 2020, 2:16pm UTC](https://discourse.julialang.org/t/how-to-define-a-sampler-for-an-object/37965/2 "2020-04-21T14:16:14Z")

</div>

> [@logankilpatrick](#):
>
> I read the docs [here](https://docs.julialang.org/en/v1/stdlib/Random/#Random.Sampler) and [here](https://docs.julialang.org/en/v1/stdlib/Random/#Custom-sampler-types-1) but it is not clear to me where I need to define the Sampler or what the syntax I should use to do so would be.

Without an MWE, it would be great if you could explain _what_ is not clear in the docs. All examples there are meant to be adapted for practical use easily.

Generally, if you need precalculated data, go with a `SamplerSimple`, otherwise a `SamplerTrivial` will do.

---

<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: [April 21, 2020, 3:00pm UTC](https://discourse.julialang.org/t/how-to-define-a-sampler-for-an-object/37965/3 "2020-04-21T15:00:50Z")

</div>

I second @Tamas_Papp’s answer, but in your example of `rand` method, the second argument is of type `VDPTagPOMDP`, is it a subtype of `Random.Sampler`? The API requires that.

---

<div class="post-metadata">

### Author: ![logankilpatrick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/logankilpatrick/32/38123_2.png) [@logankilpatrick](https://discourse.julialang.org/u/logankilpatrick)
#### Post date: [April 21, 2020, 3:21pm UTC](https://discourse.julialang.org/t/how-to-define-a-sampler-for-an-object/37965/4 "2020-04-21T15:21:18Z")

</div>

I previously had:

```julia
function rand(rng::AbstractRNG, p::VDPTagPOMDP{CardinalBarriers})
    # return TagState([0.0, 0.0], 8.0*rand(rng, 2)-4.0)
    sp = Random.Sampler(AbstractRNG, VDPTagPOMDP{CardinalBarriers}, Val(Inf))

    return TagState([0.0, 0.0], 8.0*rand(rng, sp) .- 4.0) # sp used to be 2

end

```

but I ended up getting the same error as before.

---

<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: [April 21, 2020, 4:01pm UTC](https://discourse.julialang.org/t/how-to-define-a-sampler-for-an-object/37965/5 "2020-04-21T16:01:45Z")

</div>

This other version doesn’t change the property of `VPDTagPOMDP` being a subtype of `Sampler`. It’s the second argument of the `rand` definition which must be a subtype of it.  
I don’t know what is `VPDTagPOMDP`, but if it’s not already a `Sampler`, it looks like your signature should be `rand(rng::AbstractRNG, p::Random.SamplerTrivial{VDPTagPOMDP{CardinalBarriers}})`.

---

<div class="post-metadata">

### Author: ![logankilpatrick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/logankilpatrick/32/38123_2.png) [@logankilpatrick](https://discourse.julialang.org/u/logankilpatrick)
#### Post date: [April 21, 2020, 5:04pm UTC](https://discourse.julialang.org/t/how-to-define-a-sampler-for-an-object/37965/6 "2020-04-21T17:04:32Z")

</div>

Thanks @rfourquet, I was no aware I had to do `rand(rng::AbstractRNG, p::Random.SamplerTrivial{VDPTagPOMDP{CardinalBarriers}})` but this seems to have solved my issue. Thanks again!
