Function rand::thread_rng [] [src]

pub fn thread_rng() -> ThreadRng

Retrieve the lazily-initialized thread-local random number generator, seeded by the system. Intended to be used in method chaining style, e.g. thread_rng().gen::<i32>(), or cached locally, e.g. let mut rng = thread_rng();.

ThreadRng uses ReseedingRng wrapping the same PRNG as StdRng, which is reseeded after generating 32 MiB of random data. A single instance is cached per thread and the returned ThreadRng is a reference to this instance — hence ThreadRng is neither Send nor Sync but is safe to use within a single thread. This RNG is seeded and reseeded via EntropyRng as required.

Note that the reseeding is done as an extra precaution against entropy leaks and is in theory unnecessary — to predict thread_rng's output, an attacker would have to either determine most of the RNG's seed or internal state, or crack the algorithm used.

Like StdRng, ThreadRng is a cryptographically secure PRNG. The current algorithm used is HC-128, which is an array-based PRNG that trades memory usage for better performance. This makes it similar to ISAAC, the algorithm used in ThreadRng before rand 0.5.