1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
// Based off lemire's wyrand C++ code at https://github.com/lemire/testingRNG/blob/master/source/wyrand.h

use crate::RNG_STATE_GLOBAL;
use core::sync::atomic::Ordering;

pub fn rand(state: u64) -> u64 {
	let state = state.wrapping_add(0xa0761d6478bd642f);
	let t: u128 = (state as u128).wrapping_mul((state ^ 0xe7037ed1a0b428db) as u128);
	((t >> 64) ^ t) as u64
}

pub fn rand_global() -> u64 {
	let state = RNG_STATE_GLOBAL.load(Ordering::Relaxed);
	let result = rand(state);
	RNG_STATE_GLOBAL.store(result, Ordering::Relaxed);
	result
}