Skip to content

Instantly share code, notes, and snippets.

@weihanglo
Last active August 26, 2020 07:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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 {
a: u64,
}
impl XorShift64 {
pub fn next(&mut self) -> u64 {
let mut x = self.a;
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
self.a = x;
x
}
}
fn main() {
let mut rng = XorShift64 { a: 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