Struct ThreadRng

struct ThreadRng { ... }

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:

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

impl ThreadRng

fn reseed(self: &mut Self) -> Result<(), OsError>

Immediately reseed the generator

This discards any remaining random data in the cache.

impl Clone for ThreadRng

fn clone(self: &Self) -> ThreadRng

impl CryptoRng for ThreadRng

impl Debug for ThreadRng

fn fmt(self: &Self, fmt: &mut Formatter<'_>) -> Result

impl Default for ThreadRng

fn default() -> ThreadRng

impl Freeze for ThreadRng

impl RefUnwindSafe for ThreadRng

impl RngCore for ThreadRng

fn next_u32(self: &mut Self) -> u32
fn next_u64(self: &mut Self) -> u64
fn fill_bytes(self: &mut Self, dest: &mut [u8])

impl Send for ThreadRng

impl Sync for ThreadRng

impl Unpin for ThreadRng

impl UnsafeUnpin for ThreadRng

impl UnwindSafe for ThreadRng

impl<R> Rng for ThreadRng

impl<R> TryCryptoRng for ThreadRng

impl<R> TryRngCore for ThreadRng

fn try_next_u32(self: &mut Self) -> Result<u32, <R as TryRngCore>::Error>
fn try_next_u64(self: &mut Self) -> Result<u64, <R as TryRngCore>::Error>
fn try_fill_bytes(self: &mut Self, dst: &mut [u8]) -> Result<(), <R as TryRngCore>::Error>

impl<T> Any for ThreadRng

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for ThreadRng

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for ThreadRng

fn borrow_mut(self: &mut Self) -> &mut T

impl<T> CloneToUninit for ThreadRng

unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)

impl<T> From for ThreadRng

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for ThreadRng

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T, U> Into for ThreadRng

fn into(self: Self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom for ThreadRng

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for ThreadRng

fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>

impl<V, T> VZip for ThreadRng

fn vzip(self: Self) -> V