randomize 2.2.0

A dead simple to use randomization library for rust.
Documentation
//! PCG variants where you pick the output stream.
//!
//! This is the "fundamental" version of PCG.
//!
//! The selected stream value must be odd to attain the full period. The `seed`
//! and `from` methods ensure this, though the `exact` method does not.

use super::*;

const DEFAULT_PICK_SEED: u128 = 201526561274146932589719779721328219291;
const DEFAULT_PICK_INC: u128 = 34172814569070222299;

/// A PCG with 8 bits of state, you pick the stream
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PCGPickU8 {
  state: u8,
  inc: NonZeroU8,
}
impl PCGPickU8 {
  /// Seeds the generator
  pub const fn seed(seed: u8, inc: u8) -> Self {
    let inc = unsafe { NonZeroU8::new_unchecked((inc << 1) | 1) };
    let mut out = Self { state: 0, inc };
    out.state = lcg8(out.state, PCG_DEFAULT_MULTIPLIER_8, out.inc);
    out.state = out.state.wrapping_add(seed);
    out.state = lcg8(out.state, PCG_DEFAULT_MULTIPLIER_8, out.inc);
    out
  }
  /// Gives a generator of the exact values you put in
  pub const fn exact(state: u8, inc: NonZeroU8) -> Self {
    Self { state, inc }
  }
  /// Gives the next same-size output.
  pub fn next_u8(&mut self) -> u8 {
    let output = rxs_m_xs_8_8(self.state);
    let next_state = lcg8(self.state, PCG_DEFAULT_MULTIPLIER_8, self.inc);
    self.state = next_state;
    output
  }
}
impl From<u64> for PCGPickU8 {
  fn from(seed: u64) -> Self {
    Self::seed(seed as u8, seed as u8)
  }
}
impl Default for PCGPickU8 {
  fn default() -> Self {
    Self::seed(DEFAULT_PICK_SEED as u8, DEFAULT_PICK_INC as u8)
  }
}

/// A PCG with 16 bits of state, you pick the stream
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PCGPickU16 {
  state: u16,
  inc: NonZeroU16,
}
impl PCGPickU16 {
  /// Seeds the generator
  pub const fn seed(seed: u16, inc: u16) -> Self {
    let inc = unsafe { NonZeroU16::new_unchecked((inc << 1) | 1) };
    let mut out = Self { state: 0, inc };
    out.state = lcg16(out.state, PCG_DEFAULT_MULTIPLIER_16, out.inc);
    out.state = out.state.wrapping_add(seed);
    out.state = lcg16(out.state, PCG_DEFAULT_MULTIPLIER_16, out.inc);
    out
  }
  /// Gives a generator of the exact values you put in
  pub const fn exact(state: u16, inc: NonZeroU16) -> Self {
    Self { state, inc }
  }
  /// Gives the next half-size output, using rotation
  pub fn next_u8(&mut self) -> u8 {
    let output = xsh_rr_16_8(self.state);
    let next_state = lcg16(self.state, PCG_DEFAULT_MULTIPLIER_16, self.inc);
    self.state = next_state;
    output
  }
  /// Gives the next half-size output, using a shift
  pub fn next_u8shift(&mut self) -> u8 {
    let output = xsh_rs_16_8(self.state);
    let next_state = lcg16(self.state, PCG_DEFAULT_MULTIPLIER_16, self.inc);
    self.state = next_state;
    output
  }
  /// Gives the next same-size output.
  pub fn next_u16(&mut self) -> u16 {
    let output = rxs_m_xs_16_16(self.state);
    let next_state = lcg16(self.state, PCG_DEFAULT_MULTIPLIER_16, self.inc);
    self.state = next_state;
    output
  }
}
impl From<u64> for PCGPickU16 {
  fn from(seed: u64) -> Self {
    Self::seed(seed as u16, seed as u16)
  }
}
impl Default for PCGPickU16 {
  fn default() -> Self {
    Self::seed(DEFAULT_PICK_SEED as u16, DEFAULT_PICK_INC as u16)
  }
}

/// A PCG with 32 bits of state, you pick the stream
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PCGPickU32 {
  state: u32,
  inc: NonZeroU32,
}
impl PCGPickU32 {
  /// Seeds the generator
  pub const fn seed(seed: u32, inc: u32) -> Self {
    let inc = unsafe { NonZeroU32::new_unchecked((inc << 1) | 1) };
    let mut out = Self { state: 0, inc };
    out.state = lcg32(out.state, PCG_DEFAULT_MULTIPLIER_32, out.inc);
    out.state = out.state.wrapping_add(seed);
    out.state = lcg32(out.state, PCG_DEFAULT_MULTIPLIER_32, out.inc);
    out
  }
  /// Gives a generator of the exact values you put in
  pub const fn exact(state: u32, inc: NonZeroU32) -> Self {
    Self { state, inc }
  }
  /// Gives the next half-size output, using rotation
  pub fn next_u16(&mut self) -> u16 {
    let output = xsh_rr_32_16(self.state);
    let next_state = lcg32(self.state, PCG_DEFAULT_MULTIPLIER_32, self.inc);
    self.state = next_state;
    output
  }
  /// Gives the next half-size output, using a shift
  pub fn next_u16shift(&mut self) -> u16 {
    let output = xsh_rs_32_16(self.state);
    let next_state = lcg32(self.state, PCG_DEFAULT_MULTIPLIER_32, self.inc);
    self.state = next_state;
    output
  }
  /// Gives the next same-size output.
  pub fn next_u32(&mut self) -> u32 {
    let output = rxs_m_xs_32_32(self.state);
    let next_state = lcg32(self.state, PCG_DEFAULT_MULTIPLIER_32, self.inc);
    self.state = next_state;
    output
  }
}
impl From<u64> for PCGPickU32 {
  fn from(seed: u64) -> Self {
    Self::seed(seed as u32, seed as u32)
  }
}
impl Default for PCGPickU32 {
  fn default() -> Self {
    Self::seed(DEFAULT_PICK_SEED as u32, DEFAULT_PICK_INC as u32)
  }
}

/// A PCG with 64 bits of state, you pick the stream
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PCGPickU64 {
  state: u64,
  inc: NonZeroU64,
}
impl PCGPickU64 {
  /// Seeds the generator
  pub const fn seed(seed: u64, inc: u64) -> Self {
    let inc = unsafe { NonZeroU64::new_unchecked((inc << 1) | 1) };
    let mut out = Self { state: 0, inc };
    out.state = lcg64(out.state, PCG_DEFAULT_MULTIPLIER_64, out.inc);
    out.state = out.state.wrapping_add(seed);
    out.state = lcg64(out.state, PCG_DEFAULT_MULTIPLIER_64, out.inc);
    out
  }
  /// Gives a generator of the exact values you put in
  pub const fn exact(state: u64, inc: NonZeroU64) -> Self {
    Self { state, inc }
  }
  /// Gives the next half-size output, using rotation
  pub fn next_u32(&mut self) -> u32 {
    let output = xsh_rr_64_32(self.state);
    let next_state = lcg64(self.state, PCG_DEFAULT_MULTIPLIER_64, self.inc);
    self.state = next_state;
    output
  }
  /// Gives the next half-size output, using a shift
  pub fn next_u32shift(&mut self) -> u32 {
    let output = xsh_rs_64_32(self.state);
    let next_state = lcg64(self.state, PCG_DEFAULT_MULTIPLIER_64, self.inc);
    self.state = next_state;
    output
  }
  /// Gives the next half-size output, using xsl rr
  pub fn next_u32xslrr(&mut self) -> u32 {
    let output = xsl_rr_64_32(self.state);
    let next_state = lcg64(self.state, PCG_DEFAULT_MULTIPLIER_64, self.inc);
    self.state = next_state;
    output
  }
  /// Gives the next same-size output.
  pub fn next_u64(&mut self) -> u64 {
    let output = rxs_m_xs_64_64(self.state);
    let next_state = lcg64(self.state, PCG_DEFAULT_MULTIPLIER_64, self.inc);
    self.state = next_state;
    output
  }
  /// Gives the next same-size output, using xsl rr rr
  pub fn next_u64xsl2rr(&mut self) -> u64 {
    let output = xsl_rr_rr_64_64(self.state);
    let next_state = lcg64(self.state, PCG_DEFAULT_MULTIPLIER_64, self.inc);
    self.state = next_state;
    output
  }
}
impl From<u64> for PCGPickU64 {
  fn from(seed: u64) -> Self {
    Self::seed(seed, seed)
  }
}
impl Default for PCGPickU64 {
  fn default() -> Self {
    Self::seed(DEFAULT_PICK_SEED as u64, DEFAULT_PICK_INC as u64)
  }
}

/// A PCG with 128 bits of state, you pick the stream
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PCGPickU128 {
  state: u128,
  inc: NonZeroU128,
}
impl PCGPickU128 {
  /// Seeds the generator
  pub const fn seed(seed: u128, inc: u128) -> Self {
    let inc = unsafe { NonZeroU128::new_unchecked((inc << 1) | 1) };
    let mut out = Self { state: 0, inc };
    out.state = lcg128(out.state, PCG_DEFAULT_MULTIPLIER_128, out.inc);
    out.state = out.state.wrapping_add(seed);
    out.state = lcg128(out.state, PCG_DEFAULT_MULTIPLIER_128, out.inc);
    out
  }
  /// Gives a generator of the exact values you put in
  pub const fn exact(state: u128, inc: NonZeroU128) -> Self {
    Self { state, inc }
  }
  /// Gives the next half-size output, using rotation
  pub fn next_u64(&mut self) -> u64 {
    let output = xsh_rr_128_64(self.state);
    let next_state = lcg128(self.state, PCG_DEFAULT_MULTIPLIER_128, self.inc);
    self.state = next_state;
    output
  }
  /// Gives the next half-size output, using a shift
  pub fn next_u64shift(&mut self) -> u64 {
    let output = xsh_rs_128_64(self.state);
    let next_state = lcg128(self.state, PCG_DEFAULT_MULTIPLIER_128, self.inc);
    self.state = next_state;
    output
  }
  /// Gives the next half-size output, using xsl rr
  pub fn next_u64xslrr(&mut self) -> u64 {
    let output = xsl_rr_128_64(self.state);
    let next_state = lcg128(self.state, PCG_DEFAULT_MULTIPLIER_128, self.inc);
    self.state = next_state;
    output
  }
  /// Gives the next same-size output.
  pub fn next_u128(&mut self) -> u128 {
    let output = rxs_m_xs_128_128(self.state);
    let next_state = lcg128(self.state, PCG_DEFAULT_MULTIPLIER_128, self.inc);
    self.state = next_state;
    output
  }
  /// Gives the next same-size output, using xsl rr rr
  pub fn next_u128xsl2rr(&mut self) -> u128 {
    let output = xsl_rr_rr_128_128(self.state);
    let next_state = lcg128(self.state, PCG_DEFAULT_MULTIPLIER_128, self.inc);
    self.state = next_state;
    output
  }
}
impl From<u64> for PCGPickU128 {
  fn from(seed: u64) -> Self {
    Self::seed(seed as u128, seed as u128)
  }
}
impl Default for PCGPickU128 {
  fn default() -> Self {
    Self::seed(DEFAULT_PICK_SEED, DEFAULT_PICK_INC)
  }
}