# OsRng in rand::rngs - Rust
A random number generator that retrieves randomness from the operating system.
Keywords: rust, rustlang, rust-lang, OsRng

# Struct rand::rngs::OsRng

source · [−]

    pub struct OsRng;

Expand description

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

This is a zero-sized struct. It can be freely constructed with `OsRng`.

The implementation is provided by the getrandom crate. Refer to getrandom documentation for details.

This struct is only available when specifying the crate feature `getrandom` or `std`. When using the `rand` lib, it is also available as `rand::rngs::OsRng`.

## Blocking and error handling

It is possible that when used during early boot the first call to `OsRng` will block until the system’s RNG is initialised. It is also possible (though highly unlikely) for `OsRng` to fail on some platforms, most likely due to system mis-configuration.

After the first successful call, it is highly unlikely that failures or significant delays will occur (although performance should be expected to be much slower than a user-space PRNG).

## Usage example

    use rand_core::{RngCore, OsRng};

    let mut key = [0u8; 16];
    OsRng.fill_bytes(&mut key);
    let random_u64 = OsRng.next_u64();

## Trait Implementations

source

### impl Clone for OsRng

source

#### fn clone(&self) -> OsRng

Returns a copy of the value. Read more

1.0.0 · source

#### fn clone_from(&mut self, source: &Self)

Performs copy-assignment from `source`. Read more

source

### impl Debug for OsRng

source

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

Formats the value using the given formatter. Read more

source

### impl Default for OsRng

source

#### fn default() -> OsRng

Returns the “default value” for a type. Read more

source

### impl RngCore for OsRng

source

#### fn next_u32(&mut self) -> u32

Return the next random `u32`. Read more

source

#### fn next_u64(&mut self) -> u64

Return the next random `u64`. Read more

source

#### fn fill_bytes(&mut self, dest: &mut [ u8 ])

Fill `dest` with random data. Read more

source

#### fn try_fill_bytes(&mut self, dest: &mut [ u8 ]) -> Result<(), Error>

Fill `dest` entirely with random data. Read more

source

### impl Copy for OsRng

source

### impl CryptoRng for OsRng

## Auto Trait Implementations

### impl RefUnwindSafe for OsRng

### impl Send for OsRng

### impl Sync for OsRng

### impl Unpin for OsRng

### impl UnwindSafe for OsRng

## Blanket Implementations

source

### impl<T> Any for T where
T: 'static + ?Sized,

source

#### fn type_id(&self) -> TypeId

Gets the `TypeId` of `self`. Read more

source

### impl<T> Borrow<T> for T where
T: ?Sized,

const: unstable · source

#### fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more

source

### impl<T> BorrowMut<T> for T where
T: ?Sized,

const: unstable · source

#### fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more

source

### impl<T> From<T> for T

const: unstable · source

#### fn from(t: T) -> T

Returns the argument unchanged.

source

### impl<T, U> Into<U> for T where
U: From<T>,

const: unstable · source

#### fn into(self) -> U

Calls `U::from(self)`.

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

source

### impl<T> ToOwned for T where
T: Clone,

#### type Owned = T

The resulting type after obtaining ownership.

source

#### fn to_owned(&self) -> T

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

source

#### fn clone_into(&self, target: &mut T)

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

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

source

### impl<T, U> TryFrom<U> for T where
U: Into<T>,

#### type Error = Infallible

The type returned in the event of a conversion error.

const: unstable · source

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

Performs the conversion.

source

### impl<T, U> TryInto<U> for T where
U: TryFrom<T>,

#### type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

const: unstable · source

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

Performs the conversion.

source

### impl<V, T> VZip<V> for T where
V: MultiLane<T>,

source

#### fn vzip(self) -> V