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() 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
SysRngand after every 64 kB of output. Limitation: there is no automatic reseeding on process fork (see below). - A rigorously 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.
§Forks and interrupts
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.
§Panics
Implementations of TryRng and Rng panic in case of SysRng
failure during reseeding (highly unlikely).
Implementations§
Trait Implementations§
impl TryCryptoRng for ThreadRng
Source§impl TryRng for ThreadRng
impl TryRng for ThreadRng
Source§type Error = Infallible
type Error = Infallible
Source§fn try_next_u32(&mut self) -> Result<u32, Infallible>
fn try_next_u32(&mut self) -> Result<u32, Infallible>
u32.Source§fn try_next_u64(&mut self) -> Result<u64, Infallible>
fn try_next_u64(&mut self) -> Result<u64, Infallible>
u64.Source§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.Auto Trait Implementations§
impl !RefUnwindSafe for ThreadRng
impl !Send for ThreadRng
impl !Sync for ThreadRng
impl !UnwindSafe for ThreadRng
impl Freeze for ThreadRng
impl Unpin for ThreadRng
impl UnsafeUnpin for ThreadRng
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,
impl<R> CryptoRng for R
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.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 moreimpl<R> RngCore for Rwhere
R: Rng,
Source§impl<R> RngExt for R
impl<R> RngExt 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 more