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
OsRng
and periodically thereafter (see (ReseedingRng
documentation). 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
StdRng
documentation. - Not to leak internal state through
Debug
or 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
ThreadRng
is 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 OsRng
.
§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 CryptoRng 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 !UnwindSafe 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,
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) -> DistIter<StandardUniform, Self, T> ⓘ
fn random_iter<T>(self) -> DistIter<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: 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<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_range
Rng::random_range
.Source§impl<R> TryRngCore for Rwhere
R: RngCore,
impl<R> TryRngCore for Rwhere
R: RngCore,
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 read_adapter(&mut self) -> RngReadAdapter<'_, Self>where
Self: Sized,
fn read_adapter(&mut self) -> RngReadAdapter<'_, Self>where
Self: Sized,
RngCore
to a RngReadAdapter
.