Module random
Random value generation.
This module provides two low-level interfaces for random number generation:
- The
Rngtrait abstracts over all random number generators (RNGs) and is intended to be used in all cases where the choice of RNG is left to the user. It provides thefill_bytesmethod that fills a buffer ofu8s with freshly-generated random data. - The
SystemRngimplements theRngtrait by asking the operating system for cryptographically-secure random data on every call tofill_bytes.
In the future, higher-level interfaces for features like sampling distributions
may be added to this module. Until that time, users of fill_bytes should
take care to avoid sampling bias when using the filled byte buffer to create
an instance of another type. In particular, the modulo operation is not
suitable for constraining the range of an unconstrained number:
let mut buf = [0; 2];
rng.fill_bytes(&mut buf);
// 💀 **DO NOT DO THIS** 💀
// Numbers below ca. 22000 will be twice as likely.
let very_bad_random_number = u16::from_ne_bytes(buf) % 45000;
Examples
Generating a version 4/variant 1 UUID represented as text:
use ;
println!;
Structs
- SystemRng The system random number generator.
Traits
- Distribution A trait representing a distribution of random values for a type.
- Rng A source of randomness.
Functions
- random Generates a random value from a distribution, using the default random source.