Generator from a non-uniform random distribution


#1

Hi everyone,

I am getting to know the rand crate and I have seen that an Rng as a gen_iter() method, which can be concatenated with take(n), for example, to generate n uniformly distributed numbers.

If I want to draw normally distributed numbers I can use rand::distributions::Normal, which has an ind_sample() method that takes an Rng. Is there any way to get a generator from a Normal or do I have to repeatedly call ind_sample() to get n normally distributed numbers?

Thanks,
Fernando


#2

It sounds like there’s an issue for doing this exact thing. I’m not sure if it’s been implemented though.


#3

I see, thanks. Looking at the current documentation doesn’t look like it’s been implemented yet.


#4

If you want to get a normally distributed set of numbers in the form of a generator it wouldn’t be overly difficult to write your own.

I just tried it out in the playground and managed to write a small extension trait to Rng which gives you a generator that uses some particular distribution. Feel free to copy it if it works for you.


#5

Looks good, I’ll have a closer look at it. Thanks!