Struct fastrand::Rng

source ·
pub struct Rng(/* private fields */);
Expand description

A random number generator.

Implementations§

source§

impl Rng

source

pub fn new() -> Rng

Creates a new random number generator.

source§

impl Rng

source

pub fn with_seed(seed: u64) -> Self

Creates a new random number generator with the initial seed.

source

pub fn fork(&mut self) -> Self

Clones the generator by deterministically deriving a new generator based on the initial seed.

§Example
// Seed two generators equally, and clone both of them.
let mut base1 = fastrand::Rng::new();
base1.seed(0x4d595df4d0f33173);
base1.bool(); // Use the generator once.

let mut base2 = fastrand::Rng::new();
base2.seed(0x4d595df4d0f33173);
base2.bool(); // Use the generator once.

let mut rng1 = base1.clone();
let mut rng2 = base2.clone();

assert_eq!(rng1.u64(..), rng2.u64(..), "the cloned generators are identical");
source

pub fn alphabetic(&mut self) -> char

Generates a random char in ranges a-z and A-Z.

source

pub fn alphanumeric(&mut self) -> char

Generates a random char in ranges a-z, A-Z and 0-9.

source

pub fn bool(&mut self) -> bool

Generates a random bool.

source

pub fn digit(&mut self, base: u32) -> char

Generates a random digit in the given base.

Digits are represented by chars in ranges 0-9 and a-z.

Panics if the base is zero or greater than 36.

source

pub fn f32(&mut self) -> f32

Generates a random f32 in range 0..1.

source

pub fn f64(&mut self) -> f64

Generates a random f64 in range 0..1.

source

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.

source

pub fn i8(&mut self, range: impl RangeBounds<i8>) -> i8

Generates a random i8 in the given range.

Panics if the range is empty.

source

pub fn i16(&mut self, range: impl RangeBounds<i16>) -> i16

Generates a random i16 in the given range.

Panics if the range is empty.

source

pub fn i32(&mut self, range: impl RangeBounds<i32>) -> i32

Generates a random i32 in the given range.

Panics if the range is empty.

source

pub fn i64(&mut self, range: impl RangeBounds<i64>) -> i64

Generates a random i64 in the given range.

Panics if the range is empty.

source

pub fn i128(&mut self, range: impl RangeBounds<i128>) -> i128

Generates a random i128 in the given range.

Panics if the range is empty.

source

pub fn isize(&mut self, range: impl RangeBounds<isize>) -> isize

Generates a random isize in the given range.

Panics if the range is empty.

source

pub fn lowercase(&mut self) -> char

Generates a random char in range a-z.

source

pub fn seed(&mut self, seed: u64)

Initializes this generator with the given seed.

source

pub fn get_seed(&self) -> u64

Gives back current seed that is being held by this generator.

source

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.

source

pub fn shuffle<T>(&mut self, slice: &mut [T])

Shuffles a slice randomly.

source

pub fn fill(&mut self, slice: &mut [u8])

Fill a byte slice with random data.

source

pub fn u8(&mut self, range: impl RangeBounds<u8>) -> u8

Generates a random u8 in the given range.

Panics if the range is empty.

source

pub fn u16(&mut self, range: impl RangeBounds<u16>) -> u16

Generates a random u16 in the given range.

Panics if the range is empty.

source

pub fn u32(&mut self, range: impl RangeBounds<u32>) -> u32

Generates a random u32 in the given range.

Panics if the range is empty.

source

pub fn u64(&mut self, range: impl RangeBounds<u64>) -> u64

Generates a random u64 in the given range.

Panics if the range is empty.

source

pub fn u128(&mut self, range: impl RangeBounds<u128>) -> u128

Generates a random u128 in the given range.

Panics if the range is empty.

source

pub fn usize(&mut self, range: impl RangeBounds<usize>) -> usize

Generates a random usize in the given range.

Panics if the range is empty.

source

pub fn uppercase(&mut self) -> char

Generates a random char in range A-Z.

source

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

source§

fn clone(&self) -> Rng

Clones the generator by creating a new generator with the same seed.

1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Rng

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Rng

source§

fn default() -> Rng

Initialize the Rng from the system’s random number generator.

This is equivalent to Rng::new().

source§

impl PartialEq for Rng

source§

fn eq(&self, other: &Rng) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for Rng

source§

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> 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> 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,

§

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>,

§

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>,

§

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.