Skip to content

Instantly share code, notes, and snippets.

@weihanglo
Last active January 5, 2025 13:57
Show Gist options
  • Save weihanglo/4d0c9879ccf49f64c79c1835326eb1d5 to your computer and use it in GitHub Desktop.
Save weihanglo/4d0c9879ccf49f64c79c1835326eb1d5 to your computer and use it in GitHub Desktop.
Single pseudo-random generator in Rust
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