use crate::{ConstInit, Own, Rand};
#[doc = crate::_tags!(rand)]
#[doc = crate::_doc_location!("num/prob/rand")]
#[doc = crate::_doc!(vendor: "8bit_rng")]
#[must_use]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Xyza8a {
x: u8,
y: u8,
z: u8,
a: u8,
}
impl Default for Xyza8a {
fn default() -> Self {
Self::INIT
}
}
impl ConstInit for Xyza8a {
const INIT: Self = Self::new(Self::DEFAULT_SEED);
}
impl Xyza8a {
#[doc(hidden)]
pub const DEFAULT_SEED: [u8; 4] = [0xDE, 0xFA, 0x00, 0x17];
}
impl Xyza8a {
pub const fn new(seeds: [u8; 4]) -> Self {
Self { x: seeds[0], y: seeds[1], z: seeds[2], a: seeds[3] }
}
#[must_use]
pub const fn inner_state(self) -> [u8; 4] {
[self.x, self.y, self.z, self.a]
}
pub const fn from_state(state: [u8; 4]) -> Self {
Self { x: state[0], y: state[1], z: state[2], a: state[3] }
}
#[must_use]
pub const fn current_u8(&self) -> u8 {
self.a
}
pub const fn next_u8(&mut self) -> u8 {
let t = self.x ^ (self.x << 4);
self.x = self.y;
self.y = self.z;
self.z = self.a;
self.a = self.z ^ t ^ (self.z >> 1) ^ (t << 1);
self.a
}
pub const fn peek_next_state(&self) -> Self {
let mut new = *self;
let t = new.x ^ (new.x << 4);
new.x = new.y;
new.y = new.z;
new.z = new.a;
new.a = new.z ^ t ^ (new.z >> 1) ^ (t << 1);
new
}
pub const fn own_next_u8(self) -> Own<Self, u8> {
let s = self.peek_next_state();
let v = s.current_u8();
Own::new(s, v)
}
}
impl Xyza8a {
pub const fn new1_u32(seed: u32) -> Self {
Self::new(seed.to_le_bytes())
}
pub const fn new2_u16(seeds: [u16; 2]) -> Self {
let [x, y] = seeds[0].to_le_bytes();
let [z, a] = seeds[1].to_le_bytes();
Self::new([x, y, z, a])
}
pub const fn new4_u8(seeds: [u8; 4]) -> Self {
Self::new(seeds)
}
}
#[doc = crate::_tags!(rand)]
#[doc = crate::_doc_location!("num/prob/rand")]
#[doc = crate::_doc!(vendor: "8bit_rng")]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Xyza8b {
x: u8,
y: u8,
z: u8,
a: u8,
}
impl Default for Xyza8b {
fn default() -> Self {
Self::INIT
}
}
impl ConstInit for Xyza8b {
const INIT: Self = Self::new(Self::DEFAULT_SEED);
}
impl Xyza8b {
#[doc(hidden)]
pub const DEFAULT_SEED: [u8; 4] = [0xDE, 0xFA, 0x00, 0x17];
}
impl Xyza8b {
pub const fn new(seeds: [u8; 4]) -> Self {
Self { x: seeds[0], y: seeds[1], z: seeds[2], a: seeds[3] }
}
#[must_use]
pub const fn inner_state(self) -> [u8; 4] {
[self.x, self.y, self.z, self.a]
}
pub const fn from_state(state: [u8; 4]) -> Self {
Self { x: state[0], y: state[1], z: state[2], a: state[3] }
}
pub const fn current_u8(&self) -> u8 {
self.a
}
pub const fn next_u8(&mut self) -> u8 {
let t = self.x ^ (self.x >> 1);
self.x = self.y;
self.y = self.z;
self.z = self.a;
self.a = self.z ^ t ^ (self.z >> 3) ^ (t << 1);
self.a
}
pub const fn peek_next_state(&self) -> Self {
let mut new = *self;
let t = new.x ^ (new.x >> 1);
new.x = new.y;
new.y = new.z;
new.z = new.a;
new.a = new.z ^ t ^ (new.z >> 3) ^ (t << 1);
new
}
pub const fn own_next_u8(self) -> Own<Self, u8> {
let s = self.peek_next_state();
let v = s.current_u8();
Own::new(s, v)
}
}
impl Xyza8b {
pub const fn new1_u32(seed: u32) -> Self {
Self::new(seed.to_le_bytes())
}
pub const fn new2_u16(seeds: [u16; 2]) -> Self {
let [x, y] = seeds[0].to_le_bytes();
let [z, b] = seeds[1].to_le_bytes();
Self::new([x, y, z, b])
}
pub const fn new4_u8(seeds: [u8; 4]) -> Self {
Self::new(seeds)
}
}
impl Rand for Xyza8a {
const RAND_OUTPUT_BITS: u32 = 8;
const RAND_STATE_BITS: u32 = 32;
fn rand_next_u32(&mut self) -> u32 {
u32::from_le_bytes([self.next_u8(), self.next_u8(), self.next_u8(), self.next_u8()])
}
fn rand_next_u64(&mut self) -> u64 {
u64::from_le_bytes([
self.next_u8(),
self.next_u8(),
self.next_u8(),
self.next_u8(),
self.next_u8(),
self.next_u8(),
self.next_u8(),
self.next_u8(),
])
}
fn rand_fill_bytes(&mut self, dest: &mut [u8]) {
for byte in dest {
*byte = self.next_u8();
}
}
}
impl Rand for Xyza8b {
const RAND_OUTPUT_BITS: u32 = 8;
const RAND_STATE_BITS: u32 = 32;
fn rand_next_u32(&mut self) -> u32 {
u32::from_le_bytes([self.next_u8(), self.next_u8(), self.next_u8(), self.next_u8()])
}
fn rand_next_u64(&mut self) -> u64 {
u64::from_le_bytes([
self.next_u8(),
self.next_u8(),
self.next_u8(),
self.next_u8(),
self.next_u8(),
self.next_u8(),
self.next_u8(),
self.next_u8(),
])
}
fn rand_fill_bytes(&mut self, dest: &mut [u8]) {
for byte in dest {
*byte = self.next_u8();
}
}
}
#[cfg(feature = "dep_rand_core")]
#[cfg_attr(nightly_doc, doc(cfg(feature = "dep_rand_core")))]
mod impl_rand {
use super::{Rand, Xyza8a, Xyza8b};
use crate::_dep::rand_core::{SeedableRng, TryRng};
impl TryRng for Xyza8a {
type Error = crate::Infallible;
fn try_next_u32(&mut self) -> Result<u32, Self::Error> {
Ok(self.rand_next_u32())
}
fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
Ok(self.rand_next_u64())
}
fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), Self::Error> {
self.rand_fill_bytes(dst);
Ok(())
}
}
impl SeedableRng for Xyza8a {
type Seed = [u8; 4];
fn from_seed(seeds: Self::Seed) -> Self {
Self::new(seeds)
}
}
impl TryRng for Xyza8b {
type Error = crate::Infallible;
fn try_next_u32(&mut self) -> Result<u32, Self::Error> {
Ok(self.rand_next_u32())
}
fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
Ok(self.rand_next_u64())
}
fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), Self::Error> {
self.rand_fill_bytes(dst);
Ok(())
}
}
impl SeedableRng for Xyza8b {
type Seed = [u8; 4];
fn from_seed(seeds: Self::Seed) -> Self {
Self::new(seeds)
}
}
}