A reference to the thread-local generator
This type is a reference to a lazily-initialized thread-local generator. An instance can be obtained via [rand::rng()][crate::rng()] or via [ThreadRng::default()]. The handle cannot be passed between threads (is not Send or Sync).
Security
Security must be considered relative to a threat model and validation requirements. The Rand project can provide no guarantee of fitness for purpose. The design criteria for ThreadRng are as follows:
- Automatic seeding via
OsRngand periodically thereafter (see (ReseedingRngdocumentation). Limitation: there is no automatic reseeding on process fork (see below). - A rigorusly analyzed, unpredictable (cryptographic) pseudo-random generator (see the book on security). The currently selected algorithm is ChaCha (12-rounds). See also
StdRngdocumentation. - Not to leak internal state through [
Debug] or serialization implementations. - No further protections exist to in-memory state. In particular, the implementation is not required to zero memory on exit (of the process or thread). (This may change in the future.)
- Be fast enough for general-purpose usage. Note in particular that
ThreadRngis designed to be a "fast, reasonably secure generator" (where "reasonably secure" implies the above criteria).
We leave it to the user to determine whether this generator meets their security requirements. For an alternative, see OsRng.
Fork
ThreadRng is not automatically reseeded on fork. It is recommended to explicitly call [ThreadRng::reseed] immediately after a fork, for example:
Methods on ThreadRng are not reentrant-safe and thus should not be called from an interrupt (e.g. a fork handler) unless it can be guaranteed that no other method on the same ThreadRng is currently executing.
Methods
Immediately reseed the generator
This discards any remaining random data in the cache.
Return a random u64 value via a standard uniform distribution.
Example
use ThreadRng;
let rng = rng;
let x = rng.;
println!;
Return a random i64 value via a standard uniform distribution.
Example
use ThreadRng;
let rng = rng;
let x = rng.;
println!;
Return a random char value via a standard uniform distribution.
Example
use ThreadRng;
let rng = rng;
let x = rng.;
println!;
Return a random bool value via a standard uniform distribution.
Example
use ThreadRng;
let rng = rng;
let x = rng.;
println!;
Return a random u64 value via a standard uniform constrained with a range.
Example
use ThreadRng;
let rng = rng;
let x = rng.;
println!;
Return a random i64 value via a standard uniform constrained with a range.
Example
use ThreadRng;
let rng = rng;
let x = rng.;
println!;
Return a random char value via a standard uniform constrained with a range.
Example
use ThreadRng;
let rng = rng;
let x = rng.;
println!;