Struct Rng
pub struct Rng(/* private field */);
A random number generator.
Implementations
impl Rng
const fn with_seed(seed: u64) -> SelfCreates a new random number generator with the initial seed.
fn fork(&mut self) -> SelfClones the generator by deterministically deriving a new generator based on the initial seed.
This function can be used to create a new generator that is a "spinoff" of the old generator. The new generator will not produce the same sequence of values as the old generator.
Example
// Seed two generators equally, and clone both of them. let mut base1 = fastrand::Rng::with_seed(0x4d595df4d0f33173); base1.bool(); // Use the generator once. let mut base2 = fastrand::Rng::with_seed(0x4d595df4d0f33173); base2.bool(); // Use the generator once. let mut rng1 = base1.fork(); let mut rng2 = base2.fork(); println!("rng1 returns {}", rng1.u32(..)); println!("rng2 returns {}", rng2.u32(..));fn alphabetic(&mut self) -> charGenerates a random
charin range a-z and A-Z.fn alphanumeric(&mut self) -> charGenerates a random
charin range a-z, A-Z and 0-9.fn bool(&mut self) -> boolGenerates a random
bool.fn digit(&mut self, base: u32) -> charGenerates a random digit in the given
base.Digits are represented by
chars in range 0-9 and a-z.Panics if the base is zero or greater than 36.
fn f32_inclusive(&mut self) -> f32Generates a random
f32in range0..=1.fn f32(&mut self) -> f32Generates a random
f32in range0..1.Function
f32_inclusive()is a little simpler and faster, so default to that if inclusive range is acceptable.fn f64_inclusive(&mut self) -> f64Generates a random
f64in range0..=1.fn f64(&mut self) -> f64Generates a random
f64in range0..1.Function
f64_inclusive()is a little simpler and faster, so default to that if inclusive range is acceptable.fn choose_multiple<I: IntoIterator>(&mut self, source: I, amount: usize) -> Vec<I::Item>Collects
amountvalues at random from the iterable into a vector.The length of the returned vector equals
amountunless the iterable contains insufficient elements, in which case it equals the number of elements available.Complexity is
O(n)wherenis the length of the iterable.fn i8(&mut self, range: impl RangeBounds<i8>) -> i8Generates a random
i8in the given range.Panics if the range is empty.
fn i16(&mut self, range: impl RangeBounds<i16>) -> i16Generates a random
i16in the given range.Panics if the range is empty.
fn i32(&mut self, range: impl RangeBounds<i32>) -> i32Generates a random
i32in the given range.Panics if the range is empty.
fn i64(&mut self, range: impl RangeBounds<i64>) -> i64Generates a random
i64in the given range.Panics if the range is empty.
fn i128(&mut self, range: impl RangeBounds<i128>) -> i128Generates a random
i128in the given range.Panics if the range is empty.
fn isize(&mut self, range: impl RangeBounds<isize>) -> isizeGenerates a random
isizein the given range.Panics if the range is empty.
fn lowercase(&mut self) -> charGenerates a random
charin range a-z.fn seed(&mut self, seed: u64)Initializes this generator with the given seed.
fn get_seed(&self) -> u64Gives back current seed that is being held by this generator.
fn choice<I>(&mut self, iter: I) -> Option<I::Item> where I: IntoIterator, I::IntoIter: ExactSizeIterator,Choose an item from an iterator at random.
This function may have an unexpected result if the
len()property of the iterator does not match the actual number of items in the iterator. If the iterator is empty, this returnsNone.fn shuffle<T>(&mut self, slice: &mut [T])Shuffles a slice randomly.
fn fill(&mut self, slice: &mut [u8])Fill a byte slice with random data.
fn u8(&mut self, range: impl RangeBounds<u8>) -> u8Generates a random
u8in the given range.Panics if the range is empty.
fn u16(&mut self, range: impl RangeBounds<u16>) -> u16Generates a random
u16in the given range.Panics if the range is empty.
fn u32(&mut self, range: impl RangeBounds<u32>) -> u32Generates a random
u32in the given range.Panics if the range is empty.
fn u64(&mut self, range: impl RangeBounds<u64>) -> u64Generates a random
u64in the given range.Panics if the range is empty.
fn u128(&mut self, range: impl RangeBounds<u128>) -> u128Generates a random
u128in the given range.Panics if the range is empty.
fn usize(&mut self, range: impl RangeBounds<usize>) -> usizeGenerates a random
usizein the given range.Panics if the range is empty.
fn uppercase(&mut self) -> charGenerates a random
charin range A-Z.fn char(&mut self, range: impl RangeBounds<char>) -> charGenerates a random
charin the given range.Panics if the range is empty.
impl Rng
fn new() -> RngCreates a new random number generator.
Trait Implementations
impl Clone for Rng
fn clone(&self) -> RngClones the generator by creating a new generator with the same seed.
impl Debug for Rng
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl Default for Rng
fn default() -> RngInitialize the
Rngfrom the system's random number generator.This is equivalent to [
Rng::new()].
impl Eq for Rng
impl PartialEq for Rng
fn eq(&self, other: &Self) -> bool