pub trait Random: RandomImpl {
Show 23 methods
// Provided methods
fn random<T: FromRandom>(&mut self) -> T
where Self: Sized { ... }
fn random_f64(&mut self) -> f64 { ... }
fn random_f32(&mut self) -> f32 { ... }
fn random_u128(&mut self) -> u128 { ... }
fn random_u16(&mut self) -> u16 { ... }
fn random_u8(&mut self) -> u8 { ... }
fn random_bool(&mut self) -> bool { ... }
fn random_fill<T: FromRandom>(&mut self, dst: &mut [T])
where Self: Sized { ... }
fn random_fill_uninit<T: FromRandom>(&mut self, dst: &mut [MaybeUninit<T>])
where Self: Sized { ... }
fn random_u128_bound(&mut self, bound: u128) -> u128 { ... }
fn random_u64_bound(&mut self, bound: u64) -> u64 { ... }
fn random_u32_bound(&mut self, bound: u32) -> u32 { ... }
fn random_u16_bound(&mut self, bound: u16) -> u16 { ... }
fn random_u8_bound(&mut self, bound: u8) -> u8 { ... }
fn random_range(&mut self, range: Range<f64>) -> f64 { ... }
fn random_into_iter<T: FromRandom>(self) -> Iter<T, Self> ⓘ
where Self: Sized { ... }
fn random_iter<T: FromRandom>(&mut self) -> Iter<T, &mut Self> ⓘ
where Self: Sized { ... }
fn random_into_buffer<T: FromRandom, const N: usize>(
self,
) -> Buffer<T, N, Self>
where Self: Sized { ... }
fn random_buffer<T: FromRandom, const N: usize>(
&mut self,
) -> Buffer<T, N, &mut Self>
where Self: Sized { ... }
fn random_into_buffer8<const N: usize>(self) -> Buffer8<N, Self>
where Self: Sized { ... }
fn random_buffer8<const N: usize>(&mut self) -> Buffer8<N, &mut Self>
where Self: Sized { ... }
fn random_into_crush<const N: usize>(
self,
hasher: impl Hasher,
) -> Crush<N, Self, impl Hasher>
where Self: Sized { ... }
fn random_crush<const N: usize>(
&mut self,
hasher: impl Hasher,
) -> Crush<N, &mut Self, impl Hasher>
where Self: Sized { ... }
}Expand description
generic random number generation.
this type is dyn-compatible, and implemented for all generators in this crate, allowing for very easy composition of algorithms. this trait also provides multiple common utilities for working with the generators.
implementing Random is done via RandomImpl, where you’d implement
RandomImpl::random_u64(), RandomImpl::random_u32(), and
RandomImpl::random_bytes(). Random is free, as Random is blanket
implemented for RandomImpl.
see the crate::common module for helper methods.
Provided Methods§
Sourcefn random<T: FromRandom>(&mut self) -> Twhere
Self: Sized,
fn random<T: FromRandom>(&mut self) -> Twhere
Self: Sized,
returns a new random value T.
implement FromRandom to have this method work on your own types.
§examples
use prrng::Random;
use prrng::XorShift64;
let mut rng = XorShift64::new(1);
let a: u64 = rng.random();
let a = rng.random::<i8>();
let (a, b): (u32, u16) = rng.random();
let a = rng.random::<[u8; 64]>();this method can be particularly convenient when initializing an rng with another rng:
use prrng::Random;
use prrng::SplitMix64;
use prrng::XorShift256ss;
let rng = XorShift256ss::new({
let mut temp = SplitMix64::new(24935945);
temp.random()
});Sourcefn random_f64(&mut self) -> f64
fn random_f64(&mut self) -> f64
returns a new f64.
Sourcefn random_f32(&mut self) -> f32
fn random_f32(&mut self) -> f32
returns a new f32.
Sourcefn random_u128(&mut self) -> u128
fn random_u128(&mut self) -> u128
returns a new u128.
Sourcefn random_u16(&mut self) -> u16
fn random_u16(&mut self) -> u16
returns a new u16.
Sourcefn random_bool(&mut self) -> bool
fn random_bool(&mut self) -> bool
returns a new bool.
Sourcefn random_fill<T: FromRandom>(&mut self, dst: &mut [T])where
Self: Sized,
fn random_fill<T: FromRandom>(&mut self, dst: &mut [T])where
Self: Sized,
fill a buffer with random values T.
Sourcefn random_fill_uninit<T: FromRandom>(&mut self, dst: &mut [MaybeUninit<T>])where
Self: Sized,
fn random_fill_uninit<T: FromRandom>(&mut self, dst: &mut [MaybeUninit<T>])where
Self: Sized,
fill an uninitiaized buffer with random values T.
by the end of this method, dst will be fully initialized.
Sourcefn random_u128_bound(&mut self, bound: u128) -> u128
fn random_u128_bound(&mut self, bound: u128) -> u128
returns a new u128, uniformly distributed within 0 .. bound.
Sourcefn random_u64_bound(&mut self, bound: u64) -> u64
fn random_u64_bound(&mut self, bound: u64) -> u64
returns a new u128, uniformly distributed within 0 .. bound.
Sourcefn random_u32_bound(&mut self, bound: u32) -> u32
fn random_u32_bound(&mut self, bound: u32) -> u32
returns a new u128, uniformly distributed within 0 .. bound.
Sourcefn random_u16_bound(&mut self, bound: u16) -> u16
fn random_u16_bound(&mut self, bound: u16) -> u16
returns a new u128, uniformly distributed within 0 .. bound.
Sourcefn random_u8_bound(&mut self, bound: u8) -> u8
fn random_u8_bound(&mut self, bound: u8) -> u8
returns a new u128, uniformly distributed within 0 .. bound.
Sourcefn random_range(&mut self, range: Range<f64>) -> f64
fn random_range(&mut self, range: Range<f64>) -> f64
returns a new f64, uniformly distributed within range.
Sourcefn random_into_iter<T: FromRandom>(self) -> Iter<T, Self> ⓘwhere
Self: Sized,
fn random_into_iter<T: FromRandom>(self) -> Iter<T, Self> ⓘwhere
Self: Sized,
consume self, wrapping it in an iterator crate::Iter. its Iterator::next() returns T.
Sourcefn random_iter<T: FromRandom>(&mut self) -> Iter<T, &mut Self> ⓘwhere
Self: Sized,
fn random_iter<T: FromRandom>(&mut self) -> Iter<T, &mut Self> ⓘwhere
Self: Sized,
wrap &mut self in an iterator crate::Iter. its Iterator::next() returns T.
Sourcefn random_into_buffer<T: FromRandom, const N: usize>(self) -> Buffer<T, N, Self>where
Self: Sized,
fn random_into_buffer<T: FromRandom, const N: usize>(self) -> Buffer<T, N, Self>where
Self: Sized,
consume self, wrapping it in a [crate::buffer::Buffer] with size N.
Sourcefn random_buffer<T: FromRandom, const N: usize>(
&mut self,
) -> Buffer<T, N, &mut Self>where
Self: Sized,
fn random_buffer<T: FromRandom, const N: usize>(
&mut self,
) -> Buffer<T, N, &mut Self>where
Self: Sized,
wrap &mut self in a [crate::buffer::Buffer] with size N.
Sourcefn random_into_buffer8<const N: usize>(self) -> Buffer8<N, Self>where
Self: Sized,
fn random_into_buffer8<const N: usize>(self) -> Buffer8<N, Self>where
Self: Sized,
consume self, wrapping it in a [crate::buffer::Buffer8] with size N.
Sourcefn random_buffer8<const N: usize>(&mut self) -> Buffer8<N, &mut Self>where
Self: Sized,
fn random_buffer8<const N: usize>(&mut self) -> Buffer8<N, &mut Self>where
Self: Sized,
wrap &mut self in a [crate::buffer::Buffer8] with size N.
Sourcefn random_into_crush<const N: usize>(
self,
hasher: impl Hasher,
) -> Crush<N, Self, impl Hasher>where
Self: Sized,
fn random_into_crush<const N: usize>(
self,
hasher: impl Hasher,
) -> Crush<N, Self, impl Hasher>where
Self: Sized,
consume self, wrapping it in a crate::Crush, where N is how many
hashes are run per value.
Sourcefn random_crush<const N: usize>(
&mut self,
hasher: impl Hasher,
) -> Crush<N, &mut Self, impl Hasher>where
Self: Sized,
fn random_crush<const N: usize>(
&mut self,
hasher: impl Hasher,
) -> Crush<N, &mut Self, impl Hasher>where
Self: Sized,
wrap &mut self in a crate::Crush, where N is how many
hashes are run per value.