shuffle

Function shuffle 

Source
pub fn shuffle<T>(collection: &[T]) -> Vec<T>
where T: Clone,
Expand description

Shuffle a collection, returning a new vector with the elements in random order.

This function takes a slice of items and returns a new Vec<T> containing all the elements from the input collection rearranged in a random order. It utilizes the Fisher-Yates algorithm in conjunction with random number generation functions from common.rs.

Note: This implementation relies on the random functions provided in common.rs and is not suitable for cryptographic purposes. For more robust randomness, consider using external crates like rand.

Time Complexity:
O(n), where n is the number of elements in the collection.

§Arguments

  • collection - A slice of items to be shuffled.

§Type Parameters

  • T - The type of elements in the collection. Must implement Clone.

§Returns

  • Vec<T> - A new vector containing all elements from the input collection in shuffled order.

§Examples

use lowdash::shuffle;

let numbers = vec![1, 2, 3, 4, 5];
let shuffled = shuffle(&numbers);
assert_eq!(shuffled.len(), numbers.len());
assert!(shuffled.contains(&1));
assert!(shuffled.contains(&2));
assert!(shuffled.contains(&3));
assert!(shuffled.contains(&4));
assert!(shuffled.contains(&5));