Last active
April 2, 2020 17:00
-
-
Save thyeem/f33b180b8ae1465ab0bc8adbc867b6d8 to your computer and use it in GitHub Desktop.
impl shuffle fn based on Fisher-Yates
This file contains hidden or 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
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