# Building random generator for custom type to generate a specific number of variates

**URL:** https://discourse.julialang.org/t/building-random-generator-for-custom-type-to-generate-a-specific-number-of-variates/100199
**Category:** Statistics
**Created:** [June 11, 2023, 8:54pm UTC](https://discourse.julialang.org/t/building-random-generator-for-custom-type-to-generate-a-specific-number-of-variates/100199 "2023-06-11T20:54:36Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![atteson](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@atteson](https://discourse.julialang.org/u/atteson)
#### Post date: [June 11, 2023, 8:54pm UTC](https://discourse.julialang.org/t/building-random-generator-for-custom-type-to-generate-a-specific-number-of-variates/100199/1 "2023-06-11T20:54:36Z")

</div>

I’m writing a random generator for a custom type and, for this type, it is important to know the number of variates being generated for efficient generation. My thought is to generalize Random.Repitition to allow Val{n} for some integer n in defining the sampler for this type. I would love to hear if there are other thoughts about this in the community.

---

<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: [June 12, 2023, 11:40am UTC](https://discourse.julialang.org/t/building-random-generator-for-custom-type-to-generate-a-specific-number-of-variates/100199/2 "2023-06-12T11:40:55Z")

</div>

It’s a bit hard to tell without more details, but generalizing `Random.Repetition` looks like it could work, but would be limited in that the code you don’t write yourself won’t know about it, so won’t take advantage of it. For example, `rand(your_rng, 8)` will still by default use `Val(Inf)` as the repetition and not `Val(8)`, unless you specifically define this method. In other words, you will have to define yourself all rand-related methods which should take advantage of this customization.

As a side note, if you want to specialize for many different values of `repetition`, using a straight `Int` rather than one wrapped in `Val` will be way easier on the compiler.

If there was demand for that, it might be possible to extend `Random` to make calls of the form `Sampler(rng, x, repetition::Int)` and forward that by default to `Sampler(rng, x, Val(Inf))` (so that it’s not breaking), and then implementers would be able to intercept this and define their sampler for integers.

---

<div class="post-metadata">

### Author: ![atteson](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@atteson](https://discourse.julialang.org/u/atteson)
#### Post date: [June 12, 2023, 2:14pm UTC](https://discourse.julialang.org/t/building-random-generator-for-custom-type-to-generate-a-specific-number-of-variates/100199/3 "2023-06-12T14:14:02Z")

</div>

Thanks. Reading further through the section “Hooking into the Random API” in the documentation, it indicates that, for “Creating new generators”, to specialize array generation, one should override `rand!( rng, a, sampler )`. The would be another way to go and I’m exploring this. Even though it indicates this for new generators, I’m defining my own Sampler type so this should work.
