Last active
January 5, 2025 13:57
-
-
Save weihanglo/4d0c9879ccf49f64c79c1835326eb1d5 to your computer and use it in GitHub Desktop.
Single pseudo-random generator in Rust
This file contains 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 XorShift64(u64); | |
impl XorShift64 { | |
pub const fn next(&mut self) -> u64 { | |
let mut x = self.0; | |
x ^= x << 13; | |
x ^= x >> 7; | |
x ^= x << 17; | |
self.0 = x; | |
x | |
} | |
} | |
fn main() { | |
let mut rng = XorShift64(123456789); | |
for _ in 0..10 { | |
println!("{}", rng.next()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment