Created
August 27, 2020 04:12
-
-
Save Kixiron/e1fe04529c7c370271cb10ec325088d6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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