The lodash method shuffle exported as a module for randomizing array elements using Fisher-Yates algorithm.
npx @tessl/cli install tessl/npm-lodash--shuffle@4.2.0The lodash method _.shuffle exported as a module for randomizing array elements using the Fisher-Yates shuffle algorithm. This modular package provides efficient random shuffling of collections with uniform distribution of all possible permutations.
npm install lodash.shuffleconst shuffle = require('lodash.shuffle');For ES modules:
import shuffle from 'lodash.shuffle';Note: This is the modular lodash package - install lodash.shuffle specifically, not the full lodash library.
const shuffle = require('lodash.shuffle');
// Shuffle an array
const numbers = [1, 2, 3, 4, 5];
const shuffled = shuffle(numbers);
console.log(shuffled); // [3, 1, 5, 2, 4] (random order)
// Shuffle object values
const obj = { a: 1, b: 2, c: 3 };
const shuffledValues = shuffle(obj);
console.log(shuffledValues); // [2, 3, 1] (random order of values)
// Shuffle string characters
const text = "hello";
const shuffledChars = shuffle(text);
console.log(shuffledChars); // ['e', 'l', 'o', 'h', 'l'] (random order)Creates an array of shuffled values using a version of the Fisher-Yates shuffle algorithm, ensuring uniform distribution of all possible permutations.
/**
* Creates an array of shuffled values, using a version of the Fisher-Yates shuffle
* @param {Array|Object|string} collection - The collection to shuffle
* @returns {Array} Returns the new shuffled array
*/
function shuffle(collection);Parameters:
collection (Array|Object|string): The collection to shuffle
[][]Returns:
Algorithm:
Math.random() implementationBehavior:
Usage Examples:
// Array shuffling
const deck = ['♠A', '♠K', '♠Q', '♠J'];
const shuffledDeck = shuffle(deck);
console.log(deck); // ['♠A', '♠K', '♠Q', '♠J'] (original unchanged)
console.log(shuffledDeck); // ['♠Q', '♠A', '♠J', '♠K'] (random order)
// Object value shuffling
const colors = { primary: 'red', secondary: 'blue', accent: 'green' };
const shuffledColors = shuffle(colors);
console.log(shuffledColors); // ['blue', 'green', 'red'] (values only, random order)
// String character shuffling
const word = "shuffle";
const shuffledLetters = shuffle(word);
console.log(shuffledLetters); // ['u', 's', 'l', 'f', 'e', 'h', 'f'] (characters in random order)
// Edge cases
shuffle([]); // []
shuffle({}); // []
shuffle(42); // []
shuffle(null); // []
shuffle([1]); // [1] (single element)