spinoso_random/random/rand.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
use rand_core::{Error, RngCore, SeedableRng};
use super::{seed_to_key, Mt, Random, DEFAULT_SEED_BYTES};
impl SeedableRng for Random {
type Seed = [u8; DEFAULT_SEED_BYTES];
/// Reseed from four `u32`s.
///
/// # Examples
///
/// ```
/// use rand_core::{RngCore, SeedableRng};
/// use spinoso_random::Random;
///
/// // Default MT seed
/// let seed = 5489_u128.to_le_bytes();
/// let mut mt = Random::from_seed(seed);
/// assert_ne!(mt.next_u32(), mt.next_u32());
/// ```
#[inline]
fn from_seed(seed: Self::Seed) -> Self {
let seed = seed_to_key(seed);
let mt = Mt::new_with_key(seed.iter().copied());
Self {
mt,
seed: seed.to_vec(),
}
}
}
impl RngCore for Random {
/// Generate next `u64` output.
///
/// This function is implemented by generating two `u32`s from the RNG and
/// shifting + masking them into a `u64` output.
///
/// # Examples
///
/// ```
/// use rand_core::RngCore;
/// use spinoso_random::Random;
///
/// let mut random = Random::with_seed(33);
/// assert_ne!(random.next_u64(), random.next_u64());
/// ```
#[inline]
#[must_use]
fn next_u64(&mut self) -> u64 {
self.mt.next_u64()
}
/// Generate next `u32` output.
///
/// `u32` is the native output of the generator. This function advances the
/// RNG step counter by one.
///
/// # Examples
///
/// ```
/// use rand_core::RngCore;
/// use spinoso_random::Random;
///
/// let mut random = Random::with_seed(33);
/// assert_ne!(random.next_u32(), random.next_u32());
/// ```
#[inline]
#[must_use]
fn next_u32(&mut self) -> u32 {
self.mt.next_u32()
}
/// Fill a buffer with bytes generated from the RNG.
///
/// This method generates random `u32`s (the native output unit of the RNG)
/// until `dest` is filled.
///
/// This method may discard some output bits if `dest.len()` is not a
/// multiple of 4.
///
/// # Examples
///
/// ```
/// use rand_core::RngCore;
/// use spinoso_random::Random;
///
/// let mut random = Random::with_seed(33);
/// let mut buf = [0; 32];
/// random.fill_bytes(&mut buf);
/// assert_ne!([0; 32], buf);
/// let mut buf = [0; 31];
/// random.fill_bytes(&mut buf);
/// assert_ne!([0; 31], buf);
/// ```
#[inline]
fn fill_bytes(&mut self, dest: &mut [u8]) {
self.mt.fill_bytes(dest);
}
/// Fill a buffer with bytes generated from the RNG.
///
/// This method generates random `u32`s (the native output unit of the RNG)
/// until `dest` is filled.
///
/// This method may discard some output bits if `dest.len()` is not a
/// multiple of 4.
///
/// `try_fill_bytes` is implemented with [`fill_bytes`] and is infallible.
///
/// # Examples
///
/// ```
/// use rand_core::{Error, RngCore};
/// use spinoso_random::Random;
///
/// # fn example() -> Result<(), Error> {
/// let mut random = Random::with_seed(33);
/// let mut buf = [0; 32];
/// random.try_fill_bytes(&mut buf)?;
/// assert_ne!([0; 32], buf);
/// let mut buf = [0; 31];
/// random.try_fill_bytes(&mut buf)?;
/// assert_ne!([0; 31], buf);
/// # Ok(())
/// # }
/// # example().unwrap()
/// ```
///
/// # Errors
///
/// This method never returns an error. It is equivalent to calling the
/// infallible [`fill_bytes`] method.
///
/// [`fill_bytes`]: Random::fill_bytes
#[inline]
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.mt.fill_bytes(dest);
Ok(())
}
}