[−][src]Trait random_fast_rng::Random
This is the base trait of the crate. By implementing the required method on your random generator source
it will give you a long list of functions, the important of them is Random::gen() -> T
which will produce a random value
for every type which implements GenerateRand
(you can implement this for your own types).
Notice that some random sources can produce non byte-array values with more efficiency, so if you want to use that you can just override a provided method and use the random source directly.
If your random source is fallable in a way that can be handled please also implement fill_bytes
and handle the errors properly.
otherwise it will panic.
Example
use random_trait::{Random, GenerateRand}; #[derive(Default)] struct MyRandomGenerator { ctr: usize, } impl Random for MyRandomGenerator { type Error = (); fn try_fill_bytes(&mut self, buf: &mut [u8]) -> Result<(), Self::Error> { for e in buf.iter_mut() { *e = self.ctr as u8; self.ctr += 1; } Ok(()) } } let mut rand = MyRandomGenerator::default(); let rand_u32: u32 = rand.gen(); assert_eq!(rand_u32, 50462976); let rand_u32: u32 = rand.gen(); assert_eq!(rand_u32, 117835012);
Associated Types
type Error
The Error type, based on the source of randomness, non fallible sources can use Error=()
Required methods
fn try_fill_bytes(&mut self, buf: &mut [u8]) -> Result<(), Self::Error>
This is the main method of the trait. You should implement this on your randomness source and will the buffer with random data.
Provided methods
fn fill_bytes(&mut self, buf: &mut [u8])
Uses try_fill_bytes
but panics if returns an error.
Override if you can gracefully handle errors in the randomness source.
fn gen<T>(&mut self) -> T where
T: GenerateRand,
T: GenerateRand,
Returns a generic random value which implements GenerateRand
fn get_u8(&mut self) -> u8
Returns a random u8
number.
fn get_u16(&mut self) -> u16
Returns a random u16
number.
fn get_u32(&mut self) -> u32
Returns a random u32
number.
fn get_u64(&mut self) -> u64
Returns a random u64
number.
fn get_usize(&mut self) -> usize
Returns a random usize
number.
fn get_u128(&mut self) -> u128
Returns a random u128
number.
fn get_bool(&mut self) -> bool
Returns a random bool
with 50/50 probability.