fy-shuffle
Version:
Customizable Fisher-Yates shuffle
50 lines (28 loc) • 1.57 kB
Markdown
# [fy-shuffle: Customizable Fisher-Yates shuffle](http://el-tramo.be/fy-shuffle)
Shuffles an array using the [Fisher-Yates algorithm](http://en.wikipedia.org/wiki/Fisher–Yates_shuffle). The difference between this implementation and others out there is that
the shuffle is parameterized with a random number generator function, so it can
be used to generate predictable results (using e.g. a [Linear Congruential Generator](http://el-tramo.be/lcg-random/)).
## Installation
npm install fy-shuffle --save
## Usage
A call to the exported function returns a shuffled version of the given array.
var fyShuffle = require("fy-shuffle");
// Outputs [2, 1, 4, 3], or some permutation of it.
console.log(fyShuffle([1, 2, 3, 4]));
// Pass a custom random number generator.
// Always outputs [1, 4, 2, 3].
console.log(fyShuffle([1, 2, 3, 4], function () { return 0.5; }));
## API
### `fyShuffle(array, [random])`
Shuffles the given array.
- **`array`** - *array*
Array to shuffle
- **`random`** - *function*
The random number generator function to use. Must return a number between 0.
inclusive and 1 exclusive.
Default: `Math.random`
## Project Status
[](https://travis-ci.org/remko/fy-shuffle)
[](https://coveralls.io/r/remko/fy-shuffle?branch=master)
[
](https://ci.testling.com/remko/fy-shuffle)