pub struct Rng(/* private fields */);
Expand description
A random number generator.
Implementations§
source§impl Rng
impl Rng
sourcepub fn with_seed(seed: u64) -> Self
pub fn with_seed(seed: u64) -> Self
Creates a new random number generator with the initial seed.
sourcepub fn fork(&mut self) -> Self
pub fn fork(&mut self) -> Self
Clones 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(..));
sourcepub fn alphabetic(&mut self) -> char
pub fn alphabetic(&mut self) -> char
Generates a random char
in ranges a-z and A-Z.
sourcepub fn alphanumeric(&mut self) -> char
pub fn alphanumeric(&mut self) -> char
Generates a random char
in ranges a-z, A-Z and 0-9.
sourcepub fn digit(&mut self, base: u32) -> char
pub fn digit(&mut self, base: u32) -> char
Generates a random digit in the given base
.
Digits are represented by char
s in ranges 0-9 and a-z.
Panics if the base is zero or greater than 36.
sourcepub fn choose_multiple<T: Iterator>(
&mut self,
source: T,
amount: usize,
) -> Vec<T::Item>
pub fn choose_multiple<T: Iterator>( &mut self, source: T, amount: usize, ) -> Vec<T::Item>
Collects amount
values at random from the iterator into a vector.
The length of the returned vector equals amount
unless the iterator
contains insufficient elements, in which case it equals the number of
elements available.
Complexity is O(n)
where n
is the length of the iterator.
sourcepub fn i8(&mut self, range: impl RangeBounds<i8>) -> i8
pub fn i8(&mut self, range: impl RangeBounds<i8>) -> i8
Generates a random i8
in the given range.
Panics if the range is empty.
sourcepub fn i16(&mut self, range: impl RangeBounds<i16>) -> i16
pub fn i16(&mut self, range: impl RangeBounds<i16>) -> i16
Generates a random i16
in the given range.
Panics if the range is empty.
sourcepub fn i32(&mut self, range: impl RangeBounds<i32>) -> i32
pub fn i32(&mut self, range: impl RangeBounds<i32>) -> i32
Generates a random i32
in the given range.
Panics if the range is empty.
sourcepub fn i64(&mut self, range: impl RangeBounds<i64>) -> i64
pub fn i64(&mut self, range: impl RangeBounds<i64>) -> i64
Generates a random i64
in the given range.
Panics if the range is empty.
sourcepub fn i128(&mut self, range: impl RangeBounds<i128>) -> i128
pub fn i128(&mut self, range: impl RangeBounds<i128>) -> i128
Generates a random i128
in the given range.
Panics if the range is empty.
sourcepub fn isize(&mut self, range: impl RangeBounds<isize>) -> isize
pub fn isize(&mut self, range: impl RangeBounds<isize>) -> isize
Generates a random isize
in the given range.
Panics if the range is empty.
sourcepub fn choice<I>(&mut self, iter: I) -> Option<I::Item>
pub fn choice<I>(&mut self, iter: I) -> Option<I::Item>
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 returns None
.
sourcepub fn u8(&mut self, range: impl RangeBounds<u8>) -> u8
pub fn u8(&mut self, range: impl RangeBounds<u8>) -> u8
Generates a random u8
in the given range.
Panics if the range is empty.
sourcepub fn u16(&mut self, range: impl RangeBounds<u16>) -> u16
pub fn u16(&mut self, range: impl RangeBounds<u16>) -> u16
Generates a random u16
in the given range.
Panics if the range is empty.
sourcepub fn u32(&mut self, range: impl RangeBounds<u32>) -> u32
pub fn u32(&mut self, range: impl RangeBounds<u32>) -> u32
Generates a random u32
in the given range.
Panics if the range is empty.
sourcepub fn u64(&mut self, range: impl RangeBounds<u64>) -> u64
pub fn u64(&mut self, range: impl RangeBounds<u64>) -> u64
Generates a random u64
in the given range.
Panics if the range is empty.
sourcepub fn u128(&mut self, range: impl RangeBounds<u128>) -> u128
pub fn u128(&mut self, range: impl RangeBounds<u128>) -> u128
Generates a random u128
in the given range.
Panics if the range is empty.
sourcepub fn usize(&mut self, range: impl RangeBounds<usize>) -> usize
pub fn usize(&mut self, range: impl RangeBounds<usize>) -> usize
Generates a random usize
in the given range.
Panics if the range is empty.
sourcepub fn char(&mut self, range: impl RangeBounds<char>) -> char
pub fn char(&mut self, range: impl RangeBounds<char>) -> char
Generates a random char
in the given range.
Panics if the range is empty.
Trait Implementations§
source§impl Default for Rng
impl Default for Rng
source§fn default() -> Rng
fn default() -> Rng
Initialize the Rng
from the system’s random number generator.
This is equivalent to Rng::new()
.
impl Eq for Rng
impl StructuralPartialEq for Rng
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> 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§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)