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:

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>

Auto Trait Implementations

impl Freeze for StdRng

impl RefUnwindSafe for StdRng

impl Send for StdRng

impl Sync for StdRng

impl Unpin for StdRng

impl UnsafeUnpin for StdRng

impl UnwindSafe for StdRng

Blanket Implementations

impl<R> CryptoRng for StdRng where R: TryCryptoRng<Error = never> + ?Sized,

impl<R> Rng for StdRng where R: TryRng<Error = never> + ?Sized,

impl<R> RngCore for StdRng where R: Rng,

impl<R> RngExt for StdRng where R: Rng + ?Sized,

impl<R> TryRngCore for StdRng where R: TryRng,

impl<T> Any for StdRng where T: 'static + ?Sized,

impl<T> Borrow<T> for StdRng where T: ?Sized,

impl<T> BorrowMut<T> for StdRng where T: ?Sized,

impl<T> From<T> for StdRng

impl<T, U> Into<U> for StdRng where U: From<T>,

impl<T, U> TryFrom<U> for StdRng where U: Into<T>,

impl<T, U> TryInto<U> for StdRng where U: TryFrom<T>,