Created
June 30, 2025 08:48
-
-
Save gekomad/17633e189a6543743c94b6b664841f33 to your computer and use it in GitHub Desktop.
Linear congruential generator - Park–Miller
This file contains hidden or 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
pub struct Random { | |
pub state: u32, | |
} | |
impl Random { | |
pub fn new() -> Random { | |
Random { state: Random::new_seed() } | |
} | |
fn new_seed() -> u32 { | |
std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos() as u32 | |
} | |
pub fn update_seed(&mut self) { | |
self.state = Random::new_seed(); | |
} | |
// Park–Miller RNG | |
pub fn shuffle(&mut self) { | |
let a: u32 = 16807; | |
let c: u32 = 0; | |
for _ in 0..5 { | |
self.state = self.state.wrapping_mul(a).wrapping_add(c); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment