How are you using `rand`?

It is intended to move parts of rand back into std, and, as part of that effort, the API will be tweaked/changed to be more powerful and more flexible: in particular, the Rand trait and Rng::gen* methods will be changed.

To get a better feeling for what possible changes look like and what changes would be desirable, it'd be great to have some non-trivial examples of code using the existing APIs:

  • Rand
  • Rng::{gen, gen_iter}
  • distributions::{Sample, IndependentSample}


(You can see the initial sketch for the changes to Rand in #722, particularly "Traits" and "The Random trait", but the design has moved on slightly, theoretically solving the inefficiency drawback.)

1 Like

quickcheck is a pretty big consumer of rand, although it has pretty modest requirements. I think it mostly uses gen and gen_range.


I guess I'm particularly looking for concrete instances but get a handle on what other people are using Rand-like traits for is good too.

I generally use rand in a fairly naive way. e.g. make me a list of 10 random whatevers, do operation X with probability Y, or perturb X by some random amount. My mental model of RNG is basically just the usual random() -> [0.0, 1.0) (Uniform, independent) I've seen in several languages. So I basically just do next_f64 through the thread_local rng and add/multiply/round my way to what I want even if there's a better dedicated method for it just because I don't want to think about it and that's all I'm used to working with in other languages (stockholm'd, I guess). It's easy to convert the float range into whatever format I want with minimal artifacting and I don't have to worry about seeding or whatever. I have no interest in any notion of true/precise/secure/efficient randomness. Just want to be able to generate some numbers without thinking about it. I've never had a strong urge to impl Rand since it just means more work than a quick ad-hoc make_random_graph() function (which also handles getting the Rng and everything for me).

More rigorous general treatments of randomness I would generally feed through a higher-level API like quickcheck::Arbitrary.

Not sure if this is useful but... well that's the dirty truth.

1 Like

This is somewhat off topic, but If you are going to work on rand, I recommend to have a look at It has a lot of interesting thoughts and examples on using random number generators. The C++ example for instance contains an example where copying the RNG makes sense.

I think it makes a lot of sense to implement PCG in rand as well, given how nice its properties and how simple its implementation is.

Some RNGs (namely dSFMT) are more efficient when generating random numbers in a bulk. I think this is currently not possible using the standard rand API, however this is probably a niche application.

In terms of distributions, it would be nice to have something like in R for every distribution: A way to get the probability/density function, the distribution function and the quantile function (in addition to generating random numbers according to the distribution).

I decided to take a look back at my path tracer project (haven't touched it for months) and it's very gen and gen_range heavy. I suspect that it wouldn't need much more than that (possibly choose) if I would continue, unless there were some more handy distributions and something for picking points on circles and spheres, but that would probably be better as a separate library.

As a side note: I realized the other day that it would be nice to be able to get a copy of or reference to the current random seed of a seedable RNG. I came upon a situation where I was generating a bunch of data and wanted to cache it somehow and just saving the interesting seeds would allow me to save some space. It's probably a slim use case, so it's more like an entry to the wish list and not a requirement.

1 Like

Not sure if this is what you mean, but GitHub - codahale/ A Rust implementation of the PCG PRNG. seems to be a built pcg implementation in rust.

Edit: now compiles on beta!

There is an interesting presentation about implementing distributions in R, I think part of it applies to Rust as well.

I suspect you mean GitHub - codahale/ A Rust implementation of the PCG PRNG. :slight_smile:

Yep! Not entirely sure how that happened, thanks!

I too recommend using PCG as the RNG base. The PCG paper goes into great detail about its benefits over other RNGs.

1 Like