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: NonZeroU128) -> Self
pub const fn new(seed: NonZeroU128) -> Self
Creates a random number generator with an initial state derived by hashing the given seed.
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 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.
A probability p <= 0 or NaN is treated as 0, and a probability p >= 1
is treated as 1.
Sourcepub fn non_zero_u32(&mut self) -> NonZeroU32
pub fn non_zero_u32(&mut self) -> NonZeroU32
Samples a NonZeroU32 from the uniform distribution.
Sourcepub fn non_zero_u64(&mut self) -> NonZeroU64
pub fn non_zero_u64(&mut self) -> NonZeroU64
Samples a NonZeroU64 from the uniform distribution.
Sourcepub fn non_zero_u128(&mut self) -> NonZeroU128
pub fn non_zero_u128(&mut self) -> NonZeroU128
Samples a NonZeroU128 from the uniform distribution.
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 bounded_usize(&mut self, n: usize) -> usize
pub fn bounded_usize(&mut self, n: usize) -> usize
Samples a usize from the uniform distribution over the range 0 ... n.
The upper bound is inclusive.
Sourcepub fn range_i32(&mut self, a: i32, b: i32) -> i32
pub fn range_i32(&mut self, a: i32, b: i32) -> i32
Samples a i32 from the uniform distribution over the range a ... b.
The lower and upper bounds are inclusive, and the range can wrap around
from i32::MAX to i32::MIN.
Sourcepub fn range_i64(&mut self, a: i64, b: i64) -> i64
pub fn range_i64(&mut self, a: i64, b: i64) -> i64
Samples a i64 from the uniform distribution over the range a ... b.
The lower and upper bounds are inclusive, and the range can wrap around
from i64::MAX to i64::MIN.
Sourcepub fn range_isize(&mut self, a: isize, b: isize) -> isize
pub fn range_isize(&mut self, a: isize, b: isize) -> isize
Samples a isize from the uniform distribution over the range a ... b.
The lower and upper bounds are inclusive, and the range can wrap around
from isize::MAX to isize::MIN.
Sourcepub fn range_u32(&mut self, a: u32, b: u32) -> u32
pub fn range_u32(&mut self, a: u32, b: u32) -> u32
Samples a u32 from the uniform distribution over the range a ... b.
The lower and upper bounds are inclusive, and the range can wrap around
from u32::MAX to u32::MIN.
Sourcepub fn range_u64(&mut self, a: u64, b: u64) -> u64
pub fn range_u64(&mut self, a: u64, b: u64) -> u64
Samples a u64 from the uniform distribution over the range a ... b.
The lower and upper bounds are inclusive, and the range can wrap around
from u64::MAX to u64::MIN.
Sourcepub fn range_usize(&mut self, a: usize, b: usize) -> usize
pub fn range_usize(&mut self, a: usize, b: usize) -> usize
Samples a usize from the uniform distribution over the range a ... b.
The lower and upper bounds are inclusive, and the range can wrap around
from usize::MAX to usize::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
f32using the default rounding mode.
Every output, including zero, has a positive sign bit.
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
f64using the default rounding mode.
Every output, including zero, has a positive sign bit.
Sourcepub fn biunit_f32(&mut self) -> f32
pub fn biunit_f32(&mut self) -> f32
Samples a f32 from a distribution that approximates the uniform
distribution over the real interval [-1, 1].
The distribution is the same as the one produced by the following procedure:
- Sample a real number from the uniform distribution on [-1, 1].
- Round to the nearest multiple of 2⁻⁶².
- Round to a
f32using the default rounding mode.
Sourcepub fn biunit_f64(&mut self) -> f64
pub fn biunit_f64(&mut self) -> f64
Samples a f64 from a distribution that approximates the uniform
distribution over the real interval [-1, 1].
The distribution is the same as the one produced by the following procedure:
- Sample a real number from the uniform distribution on [-1, 1].
- Round to the nearest multiple of 2⁻⁶².
- Round to a
f64using the default rounding mode.
Sourcepub fn fill(&mut self, dst: &mut [u8])
pub fn fill(&mut self, dst: &mut [u8])
Fills the provided buffer with independent uniformly distributed bytes.
Sourcepub unsafe fn fill_unchecked(&mut self, dst: *mut u8, len: usize)
pub unsafe fn fill_unchecked(&mut self, dst: *mut u8, len: usize)
Fills the provided buffer with independent uniformly distributed bytes.
§Safety
It must be valid to write len arbitrary bytes at dst.
Sourcepub fn fill_uninit<'a>(
&mut self,
dst: &'a mut [MaybeUninit<u8>],
) -> &'a mut [u8] ⓘ
pub fn fill_uninit<'a>( &mut self, dst: &'a mut [MaybeUninit<u8>], ) -> &'a mut [u8] ⓘ
Fills the provided buffer with independent uniformly distributed bytes, returning a reference to the initialized buffer. The returned buffer is a reference the same memory and has the same length as the input buffer.
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
Available on crate feature rand_core only.
impl SeedableRng for Rng
rand_core only.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<T: Rng + ?Sized>(g: &mut T) -> Self
fn from_rng<T: Rng + ?Sized>(g: &mut T) -> Self
Rng. Read more