Skip to content

Instantly share code, notes, and snippets.

@thyeem
Last active April 2, 2020 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thyeem/f33b180b8ae1465ab0bc8adbc867b6d8 to your computer and use it in GitHub Desktop.
Save thyeem/f33b180b8ae1465ab0bc8adbc867b6d8 to your computer and use it in GitHub Desktop.
impl shuffle fn based on Fisher-Yates
extern crate rand;
use rand::Rng;
// Fisher-Yates shuffle
fn shuffle<T: Clone>(v: &mut Vec<T>) {
let mut rng = rand::thread_rng();
let s = v.len();
(0..s).for_each(|i|{
let q = rng.gen_range(0, s);
v.swap(i, q);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment