Struct Rng

Source
pub struct Rng { /* private fields */ }
Expand description

A high performance non-cryptographic random number generator.

Implementations§

Source§

impl Rng

Source

pub const fn new(seed: [u8; 15]) -> Self

Creates a random number generator with an initial state derived by hashing the given byte array.

Source

pub const fn from_u64(seed: u64) -> Self

Creates a random number generator with an initial state derived by hashing the given u64 seed.

Source

pub const fn state(&self) -> NonZeroU128

Retrieves the current state of the random number generator.

Source

pub const fn from_state(state: NonZeroU128) -> Self

Creates a random number generator with a particular initial state.

If you want to deterministically initialize a generator from a small integer or other weak seed, you should NOT use this function and should instead use Rng::new or Rng::from_u64 which hash their arguments.

Source

pub fn from_operating_system() -> Self

Creates a random number generator with a random seed retrieved from the operating system.

Source

pub fn split(&mut self) -> Self

Splits off a new random number generator that may be used along with the original.

Source

pub fn bernoulli(&mut self, p: f64) -> bool

Samples a bool from the Bernoulli distribution where true appears with probability approximately equal to p.

Probabilities p <= 0 or NaN are treated as 0, and p >= 1 are treated as 1.

Source

pub fn bool(&mut self) -> bool

Samples a bool from the uniform distribution.

Source

pub fn i32(&mut self) -> i32

Samples a i32 from the uniform distribution.

Source

pub fn i64(&mut self) -> i64

Samples a i64 from the uniform distribution.

Source

pub fn u32(&mut self) -> u32

Samples a u32 from the uniform distribution.

Source

pub fn u64(&mut self) -> u64

Samples a u64 from the uniform distribution.

Source

pub fn bounded_u32(&mut self, n: u32) -> u32

Samples a u32 from the uniform distribution over the range 0 ... n.

The upper bound is inclusive.

Source

pub fn bounded_u64(&mut self, n: u64) -> u64

Samples a u64 from the uniform distribution over the range 0 ... n.

The upper bound is inclusive.

Source

pub fn between_i32(&mut self, lo: i32, hi: i32) -> i32

Samples a i32 from the uniform distribution over the range lo ... hi.

The lower and upper bounds are inclusive, and the range can wrap around from i32::MAX to i32::MIN.

Source

pub fn between_i64(&mut self, lo: i64, hi: i64) -> i64

Samples a i64 from the uniform distribution over the range lo ... hi.

The lower and upper bounds are inclusive, and the range can wrap around from i64::MAX to i64::MIN.

Source

pub fn between_u32(&mut self, lo: u32, hi: u32) -> u32

Samples a u32 from the uniform distribution over the range lo ... hi.

The lower and upper bounds are inclusive, and the range can wrap around from u32::MAX to u32::MIN.

Source

pub fn between_u64(&mut self, lo: u64, hi: u64) -> u64

Samples a u64 from the uniform distribution over the range lo ... hi.

The lower and upper bounds are inclusive, and the range can wrap around from u64::MAX to u64::MIN.

Source

pub fn f32(&mut self) -> f32

Samples a f32 from a distribution that approximates the uniform distribution over the real interval [0, 1].

The distribution is the same as the one produced by the following procedure:

  • Sample a real number from the uniform distribution on [0, 1].
  • Round to the nearest multiple of 2⁻⁶³.
  • Round to a f32 using the default rounding mode.

A zero output will always be +0, never -0.

Source

pub fn f64(&mut self) -> f64

Samples a f64 from a distribution that approximates the uniform distribution over the real interval [0, 1].

The distribution is the same as the one produced by the following procedure:

  • Sample a real number from the uniform distribution on [0, 1].
  • Round to the nearest multiple of 2⁻⁶³.
  • Round to a f64 using the default rounding mode.

A zero output will always be +0, never -0.

Source

pub fn bytes(&mut self, dst: &mut [u8])

Fills the provided buffer with independent uniformly distributed bytes.

Source

pub fn byte_array<const N: usize>(&mut self) -> [u8; N]

Samples an array of independent uniformly distributed bytes.

Trait Implementations§

Source§

impl Clone for Rng

Source§

fn clone(&self) -> Rng

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 RngCore for Rng

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, dst: &mut [u8])

Fill dest with random data. Read more
Source§

impl SeedableRng for Rng

Source§

type Seed = [u8; 16]

Seed type, which is restricted to types mutably-dereferenceable as u8 arrays (we recommend [u8; N] for some N). Read more
Source§

fn from_seed(seed: Self::Seed) -> Self

Create a new PRNG using the given seed. Read more
Source§

fn seed_from_u64(seed: u64) -> Self

Create a new PRNG using a u64 seed. Read more
Source§

fn from_rng(rng: &mut impl RngCore) -> Self

Create a new PRNG seeded from an infallible Rng. Read more
Source§

fn try_from_rng<R>(rng: &mut R) -> Result<Self, <R as TryRngCore>::Error>
where R: TryRngCore,

Create a new PRNG seeded from a potentially fallible Rng. Read more

Auto Trait Implementations§

§

impl Freeze for Rng

§

impl RefUnwindSafe for Rng

§

impl Send for Rng

§

impl Sync for Rng

§

impl Unpin for Rng

§

impl UnwindSafe for Rng

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,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

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

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

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,

Source§

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)

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

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

Source§

type Error = Infallible

The type returned in the event of a conversion error.
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>,

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

impl<R> TryRngCore for R
where R: RngCore,

Source§

type Error = Infallible

The type returned in the event of a RNG error.
Source§

fn try_next_u32(&mut self) -> Result<u32, <R as TryRngCore>::Error>

Return the next random u32.
Source§

fn try_next_u64(&mut self) -> Result<u64, <R as TryRngCore>::Error>

Return the next random u64.
Source§

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

Fill dest entirely with random data.
Source§

fn unwrap_err(self) -> UnwrapErr<Self>
where Self: Sized,

Wrap RNG with the UnwrapErr wrapper.