https://github.com/remko/fy-shuffle
Customizable Fisher-Yates shuffle
https://github.com/remko/fy-shuffle
Last synced: 4 months ago
JSON representation
Customizable Fisher-Yates shuffle
- Host: GitHub
- URL: https://github.com/remko/fy-shuffle
- Owner: remko
- Created: 2014-11-09T19:57:14.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2015-11-24T19:05:13.000Z (over 10 years ago)
- Last Synced: 2025-01-31T14:36:26.129Z (over 1 year ago)
- Language: JavaScript
- Size: 3.91 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# [fy-shuffle: Customizable Fisher-Yates shuffle](https://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)