Rand and Sampler interface for multiple samples

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 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 something close for their multivariate and matrixvariate distributions, however it only works correctly when dims is an integer.

Any ideas? :smiley:

This is not possible with the Random library. I’ve an unpublished little package which allows to do something like

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

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.


Oh wow. What an amazing answer.

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


1 Like