shuffle-es6
v0.1.0
Published
Fisher-Yates shuffle with injectable random-number-generator as ES6 module
Downloads
9
Maintainers
Readme
Shuffle arrays in place
ES-6 module with functions for shuffling arrays (shuffle
, shuffleInplace
) implemented using the Durstenfeld improvement to the Fisher-Yates algorithm (popularized by Knuth). Uses Math.random out-of-the-box, but allows user to inject any (pseudo) random number generator that have the same contract as Math.random
. By injecting a seedable random numbergenerator the user can get seeded random shuffling that generates identical results for the same seed.
Verified to be reasonably evenly distributed by included tests.
Usage
Out-of-the-box shuffle in-place (uses Math.random)
import { shuffleInplace } from shuffle-es6
var list = [...Array(42).keys()]
shuffleInplace(list)
With another (pseudo) random number generator
import { shuffleInplace } from shuffle-es6
var list = [...Array(42).keys()]
shuffleInplace(list, Math.random)
If you want a copy and not inplace, then you can use shuffle
as it wraps shuffleInplace
makes a copy using Array.slice()
before shuffling.
import { shuffle } from shuffle-es6
const inputList = [1,2,3,4,5,6]
var shuffledCopy = shuffle(inputList)
Install
npm install --save shuffle-es6