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.
It is suggested to use a fresh seed direct from the OS as the most secure and convenient option:
let rng = StdRng::from_os_rng();
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 PartialEq for StdRng
impl PartialEq for StdRng
source§impl SeedableRng for StdRng
impl SeedableRng for StdRng
§type Seed = [u8; 32]
type Seed = [u8; 32]
u8
arrays (we recommend [u8; N]
for some N
). Read moresource§fn seed_from_u64(state: u64) -> Self
fn seed_from_u64(state: u64) -> Self
u64
seed. Read moresource§fn from_rng(rng: impl RngCore) -> Self
fn from_rng(rng: impl RngCore) -> Self
Rng
. Read moresource§fn try_from_rng<R>(rng: R) -> Result<Self, <R as TryRngCore>::Error>where
R: TryRngCore,
fn try_from_rng<R>(rng: R) -> Result<Self, <R as TryRngCore>::Error>where
R: TryRngCore,
Rng
. Read moresource§fn from_os_rng() -> Self
fn from_os_rng() -> Self
source§impl TryRngCore for StdRng
impl TryRngCore for StdRng
§type Error = Infallible
type Error = Infallible
source§fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), Self::Error>
fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), Self::Error>
dest
entirely with random data.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
.impl CryptoRng for StdRng
impl Eq for StdRng
impl StructuralPartialEq for StdRng
impl TryCryptoRng 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§
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,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<R> Rng for R
impl<R> Rng for R
source§fn random<T>(&mut self) -> Twhere
Standard: Distribution<T>,
fn random<T>(&mut self) -> Twhere
Standard: Distribution<T>,
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>,
source§fn gen_iter<T>(self) -> DistIter<Standard, Self, T> ⓘ
fn gen_iter<T>(self) -> DistIter<Standard, Self, T> ⓘ
source§fn sample<T, D: Distribution<T>>(&mut self, distr: D) -> T
fn sample<T, D: Distribution<T>>(&mut self, distr: D) -> T
source§fn sample_iter<T, D>(self, distr: D) -> DistIter<D, Self, T> ⓘwhere
D: Distribution<T>,
Self: Sized,
fn sample_iter<T, D>(self, distr: D) -> DistIter<D, Self, T> ⓘwhere
D: Distribution<T>,
Self: Sized,
source§fn gen_bool(&mut self, p: f64) -> bool
fn gen_bool(&mut self, p: f64) -> bool
p
of being true. Read moresource§fn gen_ratio(&mut self, numerator: u32, denominator: u32) -> bool
fn gen_ratio(&mut self, numerator: u32, denominator: u32) -> bool
numerator/denominator
of being
true. I.e. gen_ratio(2, 3)
has chance of 2 in 3, or about 67%, of
returning true. If numerator == denominator
, then the returned value
is guaranteed to be true
. If numerator == 0
, then the returned
value is guaranteed to be false
. Read moresource§fn gen<T>(&mut self) -> Twhere
Standard: Distribution<T>,
fn gen<T>(&mut self) -> Twhere
Standard: Distribution<T>,
random
to avoid conflict with the new gen
keyword in Rust 2024.Rng::random
.