Skip to content

Instantly share code, notes, and snippets.

@Kixiron
Created August 27, 2020 04:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kixiron/e1fe04529c7c370271cb10ec325088d6 to your computer and use it in GitHub Desktop.
Save Kixiron/e1fe04529c7c370271cb10ec325088d6 to your computer and use it in GitHub Desktop.
use core::num::Wrapping;
pub(crate) struct Pcg64 {
state: Wrapping<u128>,
increment: Wrapping<u128>,
}
impl Pcg64 {
const MULTIPLIER: Wrapping<u128> = Wrapping(6_364_136_223_846_793_005);
pub fn new(seed: u64, mut increment: u64) -> Self {
if increment % 2 != 0 {
increment += 1;
}
let (seed, increment) = (Wrapping(seed as u128), Wrapping(increment as u128));
let mut gen = Self {
state: seed + increment,
increment,
};
gen.next_u64();
gen
}
pub fn next_u64(&mut self) -> u64 {
let x = self.state;
let count = (x >> 122).0 as u32;
self.state = x * Self::MULTIPLIER + self.increment;
let x = (x ^ (x >> 64)).0 as u64;
x.rotate_right(count)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment