Struct ThreadRng
pub struct ThreadRng { /* private fields */ }Expand description
A reference to the thread-local generator
This type is a reference to a lazily-initialized thread-local generator.
An instance can be obtained via [rand::rng()][crate::rng()] or via
ThreadRng::default().
The handle cannot be passed between threads (is not Send or Sync).
§Security
Security must be considered relative to a threat model and validation
requirements. The Rand project can provide no guarantee of fitness for
purpose. The design criteria for ThreadRng are as follows:
- Automatic seeding via [
SysRng] and after every 64 kB of output. Limitation: there is no automatic reseeding on process fork (see below). - A rigorusly analyzed, unpredictable (cryptographic) pseudo-random generator
(see the book on security).
The currently selected algorithm is ChaCha (12-rounds).
See also
StdRngdocumentation. - Not to leak internal state through
Debugor serialization implementations. - No further protections exist to in-memory state. In particular, the implementation is not required to zero memory on exit (of the process or thread). (This may change in the future.)
- Be fast enough for general-purpose usage. Note in particular that
ThreadRngis designed to be a “fast, reasonably secure generator” (where “reasonably secure” implies the above criteria).
We leave it to the user to determine whether this generator meets their
security requirements. For an alternative, see [SysRng].
§Fork
ThreadRng is not automatically reseeded on fork. It is recommended to
explicitly call ThreadRng::reseed immediately after a fork, for example:
fn do_fork() {
let pid = unsafe { libc::fork() };
if pid == 0 {
// Reseed ThreadRng in child processes:
rand::rng().reseed();
}
}Methods on ThreadRng are not reentrant-safe and thus should not be called
from an interrupt (e.g. a fork handler) unless it can be guaranteed that no
other method on the same ThreadRng is currently executing.
Implementations§
Trait Implementations§
§impl TryRng for ThreadRng
impl TryRng for ThreadRng
§type Error = Infallible
type Error = Infallible
§fn try_next_u32(&mut self) -> Result<u32, Infallible>
fn try_next_u32(&mut self) -> Result<u32, Infallible>
u32.§fn try_next_u64(&mut self) -> Result<u64, Infallible>
fn try_next_u64(&mut self) -> Result<u64, Infallible>
u64.§fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Infallible>
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Infallible>
dst entirely with random data.impl TryCryptoRng for ThreadRng
Auto Trait Implementations§
impl Freeze for ThreadRng
impl !RefUnwindSafe for ThreadRng
impl !Send for ThreadRng
impl !Sync for ThreadRng
impl Unpin for ThreadRng
impl UnsafeUnpin for ThreadRng
impl !UnwindSafe for ThreadRng
Blanket Implementations§
§impl<R> TryRngCore for Rwhere
R: TryRng,
impl<R> TryRngCore for Rwhere
R: TryRng,
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<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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> Pointable for T
impl<T> Pointable for T
§impl<R> Rng for Rwhere
R: TryRng<Error = Infallible> + ?Sized,
impl<R> Rng for Rwhere
R: TryRng<Error = Infallible> + ?Sized,
§impl<R> RngExt for R
impl<R> RngExt for R
§fn random<T>(&mut self) -> Twhere
StandardUniform: Distribution<T>,
fn random<T>(&mut self) -> Twhere
StandardUniform: Distribution<T>,
StandardUniform distribution. Read more§fn random_iter<T>(self) -> Iter<StandardUniform, Self, T>where
Self: Sized,
StandardUniform: Distribution<T>,
fn random_iter<T>(self) -> Iter<StandardUniform, Self, T>where
Self: Sized,
StandardUniform: Distribution<T>,
§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>,
§fn random_bool(&mut self, p: f64) -> bool
fn random_bool(&mut self, p: f64) -> bool
p of being true. Read more§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 more§fn sample<T, D>(&mut self, distr: D) -> Twhere
D: Distribution<T>,
fn sample<T, D>(&mut self, distr: D) -> Twhere
D: Distribution<T>,
§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,
§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read more§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.