pub struct ThreadRng { /* private fields */ }
std
and std_rng
and getrandom
only.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()
.
This handle is safe to use everywhere (including thread-local destructors),
though it is recommended not to use inside a fork handler.
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, as well as “soon” after a fork on Unix — see ReseedingRng
documentation for details).
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.
Trait Implementations§
source§impl RngCore for ThreadRng
impl RngCore for ThreadRng
source§fn fill_bytes(&mut self, dest: &mut [u8])
fn fill_bytes(&mut self, dest: &mut [u8])
dest
with random data. Read moresource§fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>
dest
entirely with random data. Read moresource§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
Auto Trait Implementations§
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<R> Rng for Rwhere
R: RngCore + ?Sized,
impl<R> Rng for Rwhere R: RngCore + ?Sized,
source§fn gen<T>(&mut self) -> Twhere
Standard: Distribution<T>,
fn gen<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> ⓘwhere
Self: Sized,
Standard: Distribution<T>,
fn gen_iter<T>(self) -> DistIter<Standard, Self, T> ⓘwhere Self: Sized, Standard: Distribution<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 more