Function nois::shuffle

source ·
pub fn shuffle<T>(randomness: [u8; 32], data: Vec<T>) -> Vec<T>
Expand description

Shuffles a vector using the Fisher-Yates algorithm.

This consumes the vector of elements for efficientcy reasons. Applications that do not need the original data anymore benefit from an allocation-free in-place implementation.

Examples

Shuffle a vector of integers:

use nois::{randomness_from_str, shuffle};

let randomness = randomness_from_str("9e8e26615f51552aa3b18b6f0bcf0dae5afbe30321e8d7ea7fa51ebeb1d8fe62").unwrap();

// We are randomly shuffling a vector of integers [1,2,3,4]
let data = vec![1, 2, 3, 4];
let shuffled = shuffle(randomness, data);
// The length of the vector is the same but the order of the elements has changed
assert_eq!(shuffled.len(), 4);
assert_eq!(shuffled, vec![2, 4, 3, 1]);

Shuffle a vector of strings:

use nois::{randomness_from_str, shuffle};

let randomness = randomness_from_str("9e8e26615f51552aa3b18b6f0bcf0dae5afbe30321e8d7ea7fa51ebeb1d8fe62").unwrap();

let data = vec!["bob".to_string(), "mary".to_string(), "su".to_string(), "marc".to_string()];
let shuffled = shuffle(randomness, data);
// The length of the vector is the same but the order of the elements has changed
assert_eq!(shuffled.len(), 4);
assert_eq!(shuffled, vec!["mary".to_string(), "marc".to_string(), "su".to_string(), "bob".to_string()]);

Keep a copy of the original list

use nois::{randomness_from_str, shuffle};

let randomness = randomness_from_str("9e8e26615f51552aa3b18b6f0bcf0dae5afbe30321e8d7ea7fa51ebeb1d8fe62").unwrap();

let original = vec!["bob".to_string(), "mary".to_string(), "su".to_string(), "marc".to_string()];
let shuffled = shuffle(randomness, original.clone());
// The length of the vector is the same but the order of the elements has changed
assert_eq!(shuffled.len(), original.len());
assert_ne!(shuffled, original);