pub struct Rng { /* private fields */ }
Expand description
A high performance non-cryptographic random number generator.
Implementations§
Source§impl Rng
impl Rng
Sourcepub const fn new(seed: [u8; 15]) -> Self
pub const fn new(seed: [u8; 15]) -> Self
Creates a random number generator with an initial state derived by hashing the given byte array.
Sourcepub const fn from_u64(seed: u64) -> Self
pub const fn from_u64(seed: u64) -> Self
Creates a random number generator with an initial state derived by
hashing the given u64
seed.
Sourcepub const fn state(&self) -> NonZeroU128
pub const fn state(&self) -> NonZeroU128
Retrieves the current state of the random number generator.
Sourcepub const fn from_state(state: NonZeroU128) -> Self
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.
Sourcepub fn from_operating_system() -> Self
pub fn from_operating_system() -> Self
Creates a random number generator with a random seed retrieved from the operating system.
Sourcepub fn split(&mut self) -> Self
pub fn split(&mut self) -> Self
Splits off a new random number generator that may be used along with the original.
Sourcepub fn bernoulli(&mut self, p: f64) -> bool
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.
Sourcepub fn bounded_u32(&mut self, n: u32) -> u32
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.
Sourcepub fn bounded_u64(&mut self, n: u64) -> u64
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.
Sourcepub fn between_i32(&mut self, lo: i32, hi: i32) -> i32
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
.
Sourcepub fn between_i64(&mut self, lo: i64, hi: i64) -> i64
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
.
Sourcepub fn between_u32(&mut self, lo: u32, hi: u32) -> u32
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
.
Sourcepub fn between_u64(&mut self, lo: u64, hi: u64) -> u64
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
.
Sourcepub fn f32(&mut self) -> f32
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.
Sourcepub fn f64(&mut self) -> f64
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.
Sourcepub fn bytes(&mut self, dst: &mut [u8])
pub fn bytes(&mut self, dst: &mut [u8])
Fills the provided buffer with independent uniformly distributed bytes.
Sourcepub fn byte_array<const N: usize>(&mut self) -> [u8; N]
pub fn byte_array<const N: usize>(&mut self) -> [u8; N]
Samples an array of independent uniformly distributed bytes.
Trait Implementations§
Source§impl SeedableRng for Rng
impl SeedableRng for Rng
Source§type Seed = [u8; 16]
type Seed = [u8; 16]
u8
arrays (we recommend [u8; N]
for some N
). Read moreSource§fn seed_from_u64(seed: u64) -> Self
fn seed_from_u64(seed: u64) -> Self
u64
seed. Read moreSource§fn from_rng(rng: &mut impl RngCore) -> Self
fn from_rng(rng: &mut impl RngCore) -> Self
Rng
. Read moreSource§fn try_from_rng<R>(rng: &mut R) -> Result<Self, <R as TryRngCore>::Error>where
R: TryRngCore,
fn try_from_rng<R>(rng: &mut R) -> Result<Self, <R as TryRngCore>::Error>where
R: TryRngCore,
Rng
. Read moreAuto 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> 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<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<R> TryRngCore for Rwhere
R: RngCore,
impl<R> TryRngCore for Rwhere
R: RngCore,
Source§type Error = Infallible
type Error = Infallible
Source§fn try_next_u32(&mut self) -> Result<u32, <R as TryRngCore>::Error>
fn try_next_u32(&mut self) -> Result<u32, <R as TryRngCore>::Error>
u32
.Source§fn try_next_u64(&mut self) -> Result<u64, <R as TryRngCore>::Error>
fn try_next_u64(&mut self) -> Result<u64, <R as TryRngCore>::Error>
u64
.Source§fn try_fill_bytes(
&mut self,
dst: &mut [u8],
) -> Result<(), <R as TryRngCore>::Error>
fn try_fill_bytes( &mut self, dst: &mut [u8], ) -> Result<(), <R as TryRngCore>::Error>
dest
entirely with random data.