You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
2b-rs/bot-coreutils/src/shuffle.rs

21 lines
412 B
Rust

use rand::Rng;
use std::collections::VecDeque;
pub trait Shuffle {
fn shuffle(&mut self);
}
impl<T> Shuffle for VecDeque<T> {
/// Fisher-Yates shuffle implementation
/// for VecDeque.
fn shuffle(&mut self) {
let mut rng = rand::thread_rng();
let mut i = self.len();
while i >= 2 {
i -= 1;
self.swap(i, rng.gen_range(0..i + 1))
}
}
}