fixed my horrible manual random impl, turns out the "old SEED^3 trick" isnt used anymore for *many* good reasons

This commit is contained in:
deepCurse 2024-08-15 13:54:34 -03:00
parent c102a5e40a
commit 1709084904
Signed by: u1
GPG key ID: 0EA7B9E85212693C

View file

@ -25,8 +25,8 @@ fn main() {
SEED.store(seed, Ordering::SeqCst);
// give it some time to become a tad more "random"
for _ in 0..SEED.load(Ordering::SeqCst) {
dbg!(seeded_rpower_random());
for _ in 0..seed {
seeded_random();
}
}
@ -101,7 +101,7 @@ fn number_cruncher(iter_count: usize) {
#[cfg(feature = "rust_random")]
let rand = rand::Rng::gen_range(&mut rand::thread_rng(), 0..4);
#[cfg(not(feature = "rust_random"))]
let rand = seeded_rpower_random() % 4;
let rand = seeded_random() % 4;
match rand {
0 => {
correct_count += 1;
@ -122,15 +122,16 @@ fn number_cruncher(iter_count: usize) {
}
}
const SEED: AtomicUsize = AtomicUsize::new(0);
#[cfg(not(feature = "rust_random"))]
static SEED: AtomicUsize = AtomicUsize::new(0);
#[cfg(not(feature = "rust_random"))]
fn seeded_rpower_random() -> usize {
let mut cseed = SEED.load(Ordering::SeqCst);
cseed ^= 3;
SEED.store(cseed, Ordering::SeqCst);
cseed
pub fn seeded_random() -> usize {
SEED.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |mut random| {
random ^= random << 13;
random ^= random >> 17;
random ^= random << 5;
Some(random)
})
.unwrap()
}