Skip to content

Instantly share code, notes, and snippets.

@aita
Created January 4, 2020 11:24
Show Gist options
  • Save aita/e216df61b9ff6053182a5e044497dd0c to your computer and use it in GitHub Desktop.
Save aita/e216df61b9ff6053182a5e044497dd0c to your computer and use it in GitHub Desktop.
use rand;
fn fisher_yates_shuffle<T: PartialOrd>(v: &mut [T]) {
let len = v.len();
for i in (1..v.len()).rev() {
let j = rand::random::<usize>() % (i + 1);
v.swap(i, j);
}
}
fn main() {
let mut v = [5, 1, 4, 2, 6, 3];
fisher_yates_shuffle(&mut v);
println!("{:?}", v);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment