pub struct StdRng(/* private fields */);Expand description
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 rand_chacha 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:
let rng = StdRng::from_os_rng();Seeding via rand::rng() may be faster:
let rng = StdRng::from_rng(&mut rand::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.
See also the Random Values chapter in the book.
Trait Implementations§
Source§impl SeedableRng for StdRng
 
impl SeedableRng for StdRng
Source§type Seed = [u8; 32]
 
type Seed = [u8; 32]
u8
arrays (we recommend [u8; N] for some N). Read moreSource§fn from_seed(seed: <StdRng as SeedableRng>::Seed) -> StdRng
 
fn from_seed(seed: <StdRng as SeedableRng>::Seed) -> StdRng
Source§fn seed_from_u64(state: u64) -> Self
 
fn seed_from_u64(state: u64) -> Self
u64 seed. Read moreSource§fn from_rng(rng: &mut impl RngCore) -> Self
 
fn from_rng(rng: &mut impl RngCore) -> Self
Rng. Read moreSource§fn try_from_rng<R>(rng: &mut R) -> Result<Self, <R as TryRngCore>::Error>where
    R: TryRngCore,
 
fn try_from_rng<R>(rng: &mut R) -> Result<Self, <R as TryRngCore>::Error>where
    R: TryRngCore,
Rng. Read moreSource§fn from_os_rng() -> Self
 
fn from_os_rng() -> Self
impl CryptoRng for StdRng
impl Eq for StdRng
impl StructuralPartialEq for StdRng
Auto Trait Implementations§
impl Freeze for StdRng
impl RefUnwindSafe for StdRng
impl Send for StdRng
impl Sync for StdRng
impl Unpin for StdRng
impl UnwindSafe for StdRng
Blanket Implementations§
§impl<T> ArchivePointee for T
 
impl<T> ArchivePointee for T
§type ArchivedMetadata = ()
 
type ArchivedMetadata = ()
§fn pointer_metadata(
    _: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
 
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Source§impl<T> BorrowMut<T> for Twhere
    T: ?Sized,
 
impl<T> BorrowMut<T> for Twhere
    T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
 
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
    T: Clone,
 
impl<T> CloneToUninit for Twhere
    T: Clone,
§impl<Q, K> Equivalent<K> for Q
 
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
 
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
 
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
 
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
 
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
 
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> IntoEither for T
 
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
 
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
 
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> LayoutRaw for T
 
impl<T> LayoutRaw for T
§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
 
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2where
    T: SharedNiching<N1, N2>,
    N1: Niching<T>,
    N2: Niching<T>,
 
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2where
    T: SharedNiching<N1, N2>,
    N1: Niching<T>,
    N2: Niching<T>,
§impl<T> Pointable for T
 
impl<T> Pointable for T
§impl<T> Pointee for T
 
impl<T> Pointee for T
Source§impl<R> Rng for R
 
impl<R> Rng for R
Source§fn random<T>(&mut self) -> Twhere
    StandardUniform: Distribution<T>,
 
fn random<T>(&mut self) -> Twhere
    StandardUniform: Distribution<T>,
StandardUniform distribution. Read moreSource§fn random_iter<T>(self) -> Iter<StandardUniform, Self, T>
 
fn random_iter<T>(self) -> Iter<StandardUniform, Self, T>
Source§fn random_range<T, R>(&mut self, range: R) -> Twhere
    T: SampleUniform,
    R: SampleRange<T>,
 
fn random_range<T, R>(&mut self, range: R) -> Twhere
    T: SampleUniform,
    R: SampleRange<T>,
Source§fn random_bool(&mut self, p: f64) -> bool
 
fn random_bool(&mut self, p: f64) -> bool
p of being true. Read moreSource§fn random_ratio(&mut self, numerator: u32, denominator: u32) -> bool
 
fn random_ratio(&mut self, numerator: u32, denominator: u32) -> bool
numerator/denominator of being
true. Read moreSource§fn sample<T, D>(&mut self, distr: D) -> Twhere
    D: Distribution<T>,
 
fn sample<T, D>(&mut self, distr: D) -> Twhere
    D: Distribution<T>,
Source§fn sample_iter<T, D>(self, distr: D) -> Iter<D, Self, T>where
    D: Distribution<T>,
    Self: Sized,
 
fn sample_iter<T, D>(self, distr: D) -> Iter<D, Self, T>where
    D: Distribution<T>,
    Self: Sized,
Source§fn gen<T>(&mut self) -> Twhere
    StandardUniform: Distribution<T>,
 
fn gen<T>(&mut self) -> Twhere
    StandardUniform: Distribution<T>,
random to avoid conflict with the new gen keyword in Rust 2024.Rng::random.Source§fn gen_range<T, R>(&mut self, range: R) -> Twhere
    T: SampleUniform,
    R: SampleRange<T>,
 
fn gen_range<T, R>(&mut self, range: R) -> Twhere
    T: SampleUniform,
    R: SampleRange<T>,
random_rangeRng::random_range.Source§impl<R> TryRngCore for R
 
impl<R> TryRngCore for R
Source§type Error = Infallible
 
type Error = Infallible
Source§fn try_next_u32(&mut self) -> Result<u32, <R as TryRngCore>::Error>
 
fn try_next_u32(&mut self) -> Result<u32, <R as TryRngCore>::Error>
u32.Source§fn try_next_u64(&mut self) -> Result<u64, <R as TryRngCore>::Error>
 
fn try_next_u64(&mut self) -> Result<u64, <R as TryRngCore>::Error>
u64.Source§fn try_fill_bytes(
    &mut self,
    dst: &mut [u8],
) -> Result<(), <R as TryRngCore>::Error>
 
fn try_fill_bytes( &mut self, dst: &mut [u8], ) -> Result<(), <R as TryRngCore>::Error>
dest entirely with random data.Source§fn unwrap_mut(&mut self) -> UnwrapMut<'_, Self>
 
fn unwrap_mut(&mut self) -> UnwrapMut<'_, Self>
UnwrapMut wrapper.Source§fn read_adapter(&mut self) -> RngReadAdapter<'_, Self>where
    Self: Sized,
 
fn read_adapter(&mut self) -> RngReadAdapter<'_, Self>where
    Self: Sized,
RngCore to a RngReadAdapter.