use crate::{Cast, ConstInit, Own, Rand, xorshift_basis};
#[doc = crate::_tags!(rand)]
#[doc = crate::_doc_location!("num/prob/rand")]
#[must_use]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct XorShift32<
const BASIS: usize = 1,
const A: usize = 5,
const B: usize = 17,
const C: usize = 13,
>(u32);
impl Default for XorShift32 {
fn default() -> Self {
Self::INIT
}
}
impl ConstInit for XorShift32 {
const INIT: Self = Self::new_unchecked(Self::DEFAULT_SEED);
}
impl<const BASIS: usize, const A: usize, const B: usize, const C: usize>
XorShift32<BASIS, A, B, C>
{
#[doc(hidden)]
pub const DEFAULT_SEED: u32 = 0xDEFA_0017;
#[cold] #[allow(dead_code)] #[rustfmt::skip]
const fn cold_path_default() -> Self { Self::new_unchecked(Self::DEFAULT_SEED) }
}
impl<const BASIS: usize, const A: usize, const B: usize, const C: usize>
XorShift32<BASIS, A, B, C>
{
pub const fn new(seed: u32) -> Self {
if seed == 0 { Self::cold_path_default() } else { Self(seed) }
}
pub const fn new_unchecked(seed: u32) -> Self {
debug_assert![seed != 0, "Seed must be non-zero"];
Self(seed)
}
#[must_use]
pub const fn inner_state(self) -> u32 {
self.0
}
pub const fn from_state(state: u32) -> Self {
Self(state)
}
#[must_use]
pub const fn current_u32(&self) -> u32 {
self.0
}
#[must_use]
pub const fn next_u32(&mut self) -> u32 {
let mut x = self.0;
xorshift_basis!(x, BASIS, (A, B, C));
self.0 = x;
x
}
pub const fn peek_next_state(&self) -> Self {
let mut x = self.0;
xorshift_basis!(x, BASIS, (A, B, C));
Self(x)
}
pub const fn own_next_u32(self) -> Own<Self, u32> {
let s = self.peek_next_state();
let v = s.current_u32();
Own::new(s, v)
}
}
impl<const BASIS: usize, const A: usize, const B: usize, const C: usize>
XorShift32<BASIS, A, B, C>
{
pub const fn new1_u32(seed: u32) -> Self {
Self::new(seed)
}
pub const fn new2_u16(seeds: [u16; 2]) -> Self {
Self::new(Cast::<u32>::from_u16_le(seeds))
}
pub const fn new4_u8(seeds: [u8; 4]) -> Self {
Self::new(u32::from_le_bytes(seeds))
}
}
impl<const BASIS: usize, const A: usize, const B: usize, const C: usize> Rand
for XorShift32<BASIS, A, B, C>
{
const RAND_OUTPUT_BITS: u32 = 32;
const RAND_STATE_BITS: u32 = 32;
fn rand_next_u32(&mut self) -> u32 {
self.next_u32()
}
fn rand_next_u64(&mut self) -> u64 {
Cast::<u64>::from_u32_le([self.next_u32(), self.next_u32()])
}
fn rand_fill_bytes(&mut self, dest: &mut [u8]) {
let mut i = 0;
while i < dest.len() {
let random_u32 = self.next_u32();
let bytes = random_u32.to_le_bytes();
let remaining = dest.len() - i;
if remaining >= 4 {
dest[i..i + 4].copy_from_slice(&bytes);
i += 4;
} else {
dest[i..].copy_from_slice(&bytes[..remaining]);
break;
}
}
}
}
#[cfg(feature = "dep_rand_core")]
#[cfg_attr(nightly_doc, doc(cfg(feature = "dep_rand_core")))]
mod impl_rand {
use crate::_dep::rand_core::{SeedableRng, TryRng};
use crate::{Rand, XorShift32};
impl<const BASIS: usize, const A: usize, const B: usize, const C: usize> TryRng
for XorShift32<BASIS, A, B, C>
{
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<const BASIS: usize, const A: usize, const B: usize, const C: usize> SeedableRng
for XorShift32<BASIS, A, B, C>
{
type Seed = [u8; 4];
fn from_seed(seed: Self::Seed) -> Self {
if seed == [0; 4] {
Self::cold_path_default()
} else {
Self::new_unchecked(u32::from_le_bytes(seed))
}
}
}
}
#[doc = crate::_tags!(rand)]
#[doc = crate::_doc_location!("num/prob/rand")]
#[doc(hidden)]
#[rustfmt::skip]
#[allow(dead_code)]
pub const XOROSHIFT_32_TRIPLETS: [(u8, u8, u8); 81] = [
( 1, 3,10), ( 1, 5,16), ( 1, 5,19), ( 1, 9,29), ( 1,11, 6), ( 1,11,16),
( 1,19, 3), ( 1,21,20), ( 1,27,27), ( 2, 5,15), ( 2, 5,21), ( 2, 7, 7),
( 2, 7, 9), ( 2, 7,25), ( 2, 9,15), ( 2,15,17), ( 2,15,25), ( 2,21, 9),
( 3, 1,14), ( 3, 3,26), ( 3, 3,28), ( 3, 3,29), ( 3, 5,20), ( 3, 5,22),
( 3, 5,25), ( 3, 7,29), ( 3,13, 7), ( 3,23,25), ( 3,25,24), ( 3,27,11),
( 4, 3,17), ( 4, 3,27), ( 4, 5,15), ( 5, 3,21), ( 5, 7,22), ( 5, 9, 7),
( 5, 9,28), ( 5, 9,31), ( 5,13, 6), ( 5,15,17), ( 5,17,13), ( 5,21,12),
( 5,27, 8), ( 5,27,21), ( 5,27,25), ( 5,27,28), ( 6, 1,11), ( 6, 3,17),
( 6,17, 9), ( 6,21, 7), ( 6,21,13), ( 7, 1, 9), ( 7, 1,18), ( 7, 1,25),
( 7,13,25), ( 7,17,21), ( 7,25,12), ( 7,25,20), ( 8, 7,23), ( 8, 9,23),
( 9, 5, 1), ( 9, 5,25), ( 9,11,19), ( 9,21,16), (10, 9,21), (10, 9,25),
(11, 7,12), (11,7, 16), (11,17,13), (11,21,13), (12, 9,23), (13, 3,17),
(13, 3,27), (13,5, 19), (13,17,15), (14, 1,15), (14,13,15), (15, 1,29),
(17,15,20), (17,15,23), (17,15,26)
];