Struct StdRng
pub struct StdRng(/* private field */);
A strong, fast (amortized), non-portable RNG
This is the "standard" RNG, a generator with the following properties:
- Non-portable: any future library version may replace the algorithm and results may be platform-dependent. (For a portable version, use the chacha20 crate directly.)
- CSPRNG: statistically good quality of randomness and unpredictable
- Fast (amortized): the RNG is fast for bulk generation, but the cost of method calls is not consistent due to usage of an output buffer.
The current algorithm used is the ChaCha block cipher with 12 rounds. Please see this relevant rand issue for the discussion. This may change as new evidence of cipher security and performance becomes available.
Seeding (construction)
This generator implements the SeedableRng trait. Any method may be used,
but note that seed_from_u64 is not suitable for usage where security is
important. Also note that, even with a fixed seed, output is not portable.
Using a fresh seed direct from the OS is the most secure option:
# use rand::{SeedableRng, rngs::{StdRng, SysRng}};
let rng = StdRng::try_from_rng(&mut SysRng).unwrap();
# let _: StdRng = rng;
Seeding via rand::make_rng() or rand::rng() may be
faster:
# use rand::rngs::StdRng;
let mut rng: StdRng = rand::make_rng();
# let _ = rand::Rng::next_u32(&mut rng);
Any SeedableRng method may be used, but note that seed_from_u64 is not
suitable where security is required. See also Seeding RNGs in the book.
Generation
The generator implements Rng and thus also RngExt.
See also the Random Values chapter in the book.
Trait Implementations
impl Debug for StdRng
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl Eq for StdRng
impl PartialEq for StdRng
fn eq(&self, other: &Self) -> bool
impl SeedableRng for StdRng
type Seed = [u8; 32];fn from_seed(seed: Self::Seed) -> Self
impl StructuralPartialEq for StdRng
impl TryCryptoRng for StdRng
impl TryRng for StdRng
type Error = never;fn try_next_u32(&mut self) -> Result<u32, Infallible>fn try_next_u64(&mut self) -> Result<u64, Infallible>fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), Infallible>