5593: Code shuffle resiliently r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-07-30 13:49:02 +00:00 committed by GitHub
commit 323fd64abd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -309,8 +309,13 @@ pub fn analysis_stats(
} }
fn shuffle<T>(rng: &mut Rand32, slice: &mut [T]) { fn shuffle<T>(rng: &mut Rand32, slice: &mut [T]) {
for i in (1..slice.len()).rev() { for i in 0..slice.len() {
let idx = rng.rand_range(0..i as u32) as usize; randomize_first(rng, &mut slice[i..]);
slice.swap(idx, i) }
fn randomize_first<T>(rng: &mut Rand32, slice: &mut [T]) {
assert!(!slice.is_empty());
let idx = rng.rand_range(0..slice.len() as u32) as usize;
slice.swap(0, idx);
} }
} }