Use wyrand final v4.2 constants for gen_u64()

This commit is contained in:
Gonçalo Rica Pais da Silva 2024-04-24 10:10:10 +02:00 committed by John Nunley
parent 63175fa2e2
commit 351a708838
1 changed files with 7 additions and 2 deletions

View File

@ -148,9 +148,14 @@ impl Rng {
/// Generates a random `u64`.
#[inline]
fn gen_u64(&mut self) -> u64 {
let s = self.0.wrapping_add(0xA0761D6478BD642F);
// Constants for WyRand taken from: https://github.com/wangyi-fudan/wyhash/blob/master/wyhash.h#L151
// Updated for the final v4.2 implementation with improved constants for better entropy output.
const WY_CONST_0: u64 = 0x2d35_8dcc_aa6c_78a5;
const WY_CONST_1: u64 = 0x8bb8_4b93_962e_acc9;
let s = self.0.wrapping_add(WY_CONST_0);
self.0 = s;
let t = u128::from(s) * u128::from(s ^ 0xE7037ED1A0B428DB);
let t = u128::from(s) * u128::from(s ^ WY_CONST_1);
(t as u64) ^ (t >> 64) as u64
}