Struct StdRng

struct StdRng(_)

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};
let rng = StdRng::from_os_rng();
# let _: StdRng = rng;

Seeding via rand::rng() may be faster:

# use rand::{SeedableRng, rngs::StdRng};
let rng = StdRng::from_rng(&mut rand::rng());
# let _: StdRng = 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 generators implements RngCore and thus also [Rng][crate::Rng]. See also the Random Values chapter in the book.

Implementations

impl Clone for StdRng

fn clone(self: &Self) -> StdRng

impl CryptoRng for StdRng

impl Debug for StdRng

fn fmt(self: &Self, f: &mut $crate::fmt::Formatter<'_>) -> $crate::fmt::Result

impl Eq for StdRng

impl Freeze for StdRng

impl PartialEq for StdRng

fn eq(self: &Self, other: &StdRng) -> bool

impl RefUnwindSafe for StdRng

impl RngCore for StdRng

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

impl SeedableRng for StdRng

fn from_seed(seed: <Self as >::Seed) -> Self

impl Send for StdRng

impl StructuralPartialEq for StdRng

impl Sync for StdRng

impl Unpin for StdRng

impl UnwindSafe for StdRng

impl<R> Rng for StdRng

impl<R> TryCryptoRng for StdRng

impl<R> TryRngCore for StdRng

fn try_next_u32(self: &mut Self) -> Result<u32, <R as TryRngCore>::Error>
fn try_next_u64(self: &mut Self) -> Result<u64, <R as TryRngCore>::Error>
fn try_fill_bytes(self: &mut Self, dst: &mut [u8]) -> Result<(), <R as TryRngCore>::Error>

impl<T> Any for StdRng

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for StdRng

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for StdRng

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

impl<T> CloneToUninit for StdRng

unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)

impl<T> From for StdRng

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for StdRng

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T, U> Into for StdRng

fn into(self: 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 for StdRng

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for StdRng

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

impl<V, T> VZip for StdRng

fn vzip(self: Self) -> V