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

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.

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.

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.