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 thread_rng
or via ThreadRng::default()
.
The handle cannot be passed between threads (is not Send
or Sync
).
ThreadRng
uses the same CSPRNG as StdRng
, ChaCha12. As with
StdRng
, the algorithm may be changed, subject to reasonable expectations
of security and performance.
ThreadRng
is automatically seeded from OsRng
with periodic reseeding
(every 64 kiB — see ReseedingRng
documentation for details).
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::thread_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.
Security must be considered relative to a threat model and validation
requirements. ThreadRng
attempts to meet basic security considerations
for producing unpredictable random numbers: use a CSPRNG, use a
recommended platform-specific seed (OsRng
), and avoid
leaking internal secrets e.g. via Debug
implementation or serialization.
Memory is not zeroized on drop.
Implementations§
Trait Implementations§
source§impl TryRngCore for ThreadRng
impl TryRngCore for ThreadRng
source§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 ThreadRng
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 !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§unsafe fn clone_to_uninit(&self, dst: *mut T)
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
.