[][src]Struct rand::rngs::OsRng

pub struct OsRng(_);

A random number generator that retrieves randomness straight from the operating system.

This is the preferred external source of entropy for most applications. Commonly it is used to initialize a user-space RNG, which can then be used to generate random values with much less overhead than OsRng.

You may prefer to use EntropyRng instead of OsRng. It is unlikely, but not entirely theoretical, for OsRng to fail. In such cases EntropyRng falls back on a good alternative entropy source.

OsRng::new() is guaranteed to be very cheap (after the first successful call), and will never consume more than one file handle per process.

Platform sources

OS interface
Linux, Android getrandom system call if available, otherwise /dev/urandom after reading from /dev/random once
Windows RtlGenRandom
macOS, iOS SecRandomCopyBytes
FreeBSD kern.arandom
OpenBSD, Bitrig getentropy
NetBSD /dev/urandom after reading from /dev/random once
Dragonfly BSD /dev/random
Solaris, illumos getrandom system call if available, otherwise /dev/random
Fuchsia OS cprng_draw
Redox rand:
CloudABI random_get
Haiku /dev/random (identical to /dev/urandom)
Web browsers Crypto.getRandomValues (see Support for WebAssembly and ams.js)
Node.js crypto.randomBytes (see Support for WebAssembly and ams.js)

Rand doesn't have a blanket implementation for all Unix-like operating systems that reads from /dev/urandom. This ensures all supported operating systems are using the recommended interface and respect maximum buffer sizes.

Support for WebAssembly and ams.js

The three Emscripten targets asmjs-unknown-emscripten, wasm32-unknown-emscripten and wasm32-experimental-emscripten use Emscripten's emulation of /dev/random on web browsers and Node.js.

The bare Wasm target wasm32-unknown-unknown tries to call the javascript methods directly, using either stdweb in combination with cargo-web or wasm-bindgen depending on what features are activated for this crate.

Early boot

It is possible that early in the boot process the OS hasn't had enough time yet to collect entropy to securely seed its RNG, especially on virtual machines.

Some operating systems always block the thread until the RNG is securely seeded. This can take anywhere from a few seconds to more than a minute. Others make a best effort to use a seed from before the shutdown and don't document much.

A few, Linux, NetBSD and Solaris, offer a choice between blocking, and getting an error. With try_fill_bytes we choose to get the error (ErrorKind::NotReady), while the other methods use a blocking interface.

On Linux (when the genrandom system call is not available) and on NetBSD reading from /dev/urandom never blocks, even when the OS hasn't collected enough entropy yet. As a countermeasure we try to do a single read from /dev/random until we know the OS RNG is initialized (and store this in a global static).

Panics

OsRng is extremely unlikely to fail if OsRng::new(), and one read from it, where succesfull. But in case it does fail, only try_fill_bytes is able to report the cause. Depending on the error the other RngCore methods will retry several times, and panic in case the error remains.

Methods

impl OsRng
[src]

Create a new OsRng.

Trait Implementations

impl Clone for OsRng
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl Debug for OsRng
[src]

Formats the value using the given formatter. Read more

impl CryptoRng for OsRng
[src]

impl RngCore for OsRng
[src]

Return the next random u32. Read more

Return the next random u64. Read more

Fill dest with random data. Read more

Fill dest entirely with random data. Read more

Auto Trait Implementations

impl Send for OsRng

impl Sync for OsRng

Blanket Implementations

impl<R> Rng for R where
    R: RngCore + ?Sized
[src]

Return a random value supporting the [Standard] distribution. Read more

Generate a random value in the range [low, high), i.e. inclusive of low and exclusive of high. Read more

Sample a new value, using the given distribution. Read more

Important traits for DistIter<'a, D, R, T>

Create an iterator that generates values using the given distribution. Read more

Fill dest entirely with random bytes (uniform value distribution), where dest is any type supporting [AsByteSliceMut], namely slices and arrays over primitive integer types (i8, i16, u32, etc.). Read more

Fill dest entirely with random bytes (uniform value distribution), where dest is any type supporting [AsByteSliceMut], namely slices and arrays over primitive integer types (i8, i16, u32, etc.). Read more

Return a bool with a probability p of being true. Read more

Return a bool with a probability of 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

Deprecated since 0.6.0

: use SliceRandom::choose instead

Return a random element from values. Read more

Deprecated since 0.6.0

: use SliceRandom::choose_mut instead

Return a mutable pointer to a random element from values. Read more

Deprecated since 0.6.0

: use SliceRandom::shuffle instead

Shuffle a mutable slice in place. Read more

impl<T, U> Into for T where
    U: From<T>, 
[src]

Performs the conversion.

impl<T> ToOwned for T where
    T: Clone
[src]

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

impl<T> From for T
[src]

Performs the conversion.

impl<T, U> TryFrom for T where
    T: From<U>, 
[src]

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

🔬 This is a nightly-only experimental API. (try_from)

Performs the conversion.

impl<T> Borrow for T where
    T: ?Sized
[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut for T where
    T: ?Sized
[src]

Mutably borrows from an owned value. Read more

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

🔬 This is a nightly-only experimental API. (try_from)

Performs the conversion.

impl<T> Any for T where
    T: 'static + ?Sized
[src]

🔬 This is a nightly-only experimental API. (get_type_id)

this method will likely be replaced by an associated static

Gets the TypeId of self. Read more

impl<T> FromCast for T
[src]

Numeric cast from T to Self.

impl<T, U> Cast for T where
    U: FromCast<T>, 
[src]

Numeric cast from self to T.

impl<T, U> IntoBits for T where
    U: FromBits<T>, 
[src]

Safe lossless bitwise transmute from self to T.

impl<T> FromBits for T
[src]

Safe lossless bitwise transmute from T to Self.