# Rand and Sampler interface for multiple samples

**URL:** <https://discourse.julialang.org/t/rand-and-sampler-interface-for-multiple-samples/66770>\
**Category:** General Usage\
**Tags:** distributions, random\
**Created:** [August 21, 2021, 10:42am UTC](https://discourse.julialang.org/t/rand-and-sampler-interface-for-multiple-samples/66770 "2021-08-21T10:42:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fedario](https://avatars.discourse-cdn.com/v4/letter/f/f14d63/32.png) [@fedario](https://discourse.julialang.org/u/fedario)\
**Post date:** [August 21, 2021, 10:42am UTC](https://discourse.julialang.org/t/rand-and-sampler-interface-for-multiple-samples/66770/1 "2021-08-21T10:42:35Z")

</div>

Hi Julia community!

I’m trying to implement the `Random.rand` function for a custom distribution.  
That distribution can have different “event” sizes, meaning a single sample could be a scalar, a vector, or in general an n-dimensional array.

My goal is to implement `Random.rand(custom_dist, dims)` in such a way, that it always produces an array of size `(eventsize..., dims...)`.  
For example if my distribution has an event size of (4,), meaning a single sample is a vector of length 4 and I call `Random.rand(CustomDist(eventsize=(4,)), (2, 3)`, I want the result to be an array of size `(4, 2, 3)`.

Does the Random API provide this? I’ve read the [documentation](https://docs.julialang.org/en/v1/stdlib/Random/#Hooking-into-the-Random-API) multiple times but still don’t feel like I really get it. At first I thought the last argument to `Sampler` allowed me to hook into the `dims` parameter, but it seems to be just a flag specifying if one or multiple values should be generated.

Distributions.jl does [implement](https://github.com/JuliaStats/Distributions.jl/blob/master/src/multivariates.jl) something close for their multivariate and matrixvariate distributions, however it only works correctly when dims is an integer.

Any ideas? 😃

---

<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:** [August 21, 2021, 4:42pm UTC](https://discourse.julialang.org/t/rand-and-sampler-interface-for-multiple-samples/66770/2 "2021-08-21T16:42:27Z")

</div>

This is not possible with the `Random` library. I’ve an unpublished little [package](https://github.com/rfourquet/RandomMonad.jl) which allows to do something like

```julia
julia> rand(Fill(1:9, 2)) # Fill is a sampler for an array
2-element Vector{Int64}:
 7
 8

julia> rand(Pack(Fill(1:9, 2), 3)) # Pack is a sampler which "packs" arrays together
2×3 Matrix{Int64}:
 5 2 4
 3 7 3

```

I can try to prioritize the work for registering it if you think it would be useful to you.

---

<div class="post-metadata">

**Author:** ![fedario](https://avatars.discourse-cdn.com/v4/letter/f/f14d63/32.png) [@fedario](https://discourse.julialang.org/u/fedario)\
**Post date:** [August 21, 2021, 6:10pm UTC](https://discourse.julialang.org/t/rand-and-sampler-interface-for-multiple-samples/66770/3 "2021-08-21T18:10:14Z")

</div>

Oh wow. What an amazing answer.

And yes, your package is awesome and would be very useful to me.  
Thank you for creating it.

.
