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,

fn next_u32(&mut self) -> u32
fn next_u64(&mut self) -> u64
fn fill_bytes(&mut self, dst: &mut [u8])

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,

type Error = <R as TryRng>::Error;

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

fn type_id(&self) -> TypeId

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

fn borrow(&self) -> &T

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

fn borrow_mut(&mut self) -> &mut T

impl<T> From<T> for StdRng

fn from(t: T) -> T

Returns the argument unchanged.

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

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

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

type Error = never;
fn try_from(value: U) -> Result<T, never>

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

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>