type use of rng

This commit is contained in:
Corwin 2024-04-09 22:03:33 +01:00
parent 32eb34f226
commit c632eb4ea3
No known key found for this signature in database

View file

@ -54,8 +54,9 @@ impl Default for RandomNumberGenerator {
} }
} }
static GLOBAL_RNG: AtomicU128 = static GLOBAL_RNG: AtomicU128 = AtomicU128::new(unsafe {
AtomicU128::new(unsafe { core::mem::transmute(RandomNumberGenerator::new().state) }); core::mem::transmute::<[u32; 4], u128>(RandomNumberGenerator::new().state)
});
/// Using a global random number generator, provides the next random number /// Using a global random number generator, provides the next random number
#[must_use] #[must_use]
@ -64,7 +65,10 @@ pub fn gen() -> i32 {
let data_u32: [u32; 4] = unsafe { core::mem::transmute(data) }; let data_u32: [u32; 4] = unsafe { core::mem::transmute(data) };
let mut rng = RandomNumberGenerator { state: data_u32 }; let mut rng = RandomNumberGenerator { state: data_u32 };
let value = rng.gen(); let value = rng.gen();
GLOBAL_RNG.store(unsafe { core::mem::transmute(rng.state) }, Ordering::SeqCst); GLOBAL_RNG.store(
unsafe { core::mem::transmute::<[u32; 4], u128>(rng.state) },
Ordering::SeqCst,
);
value value
} }