fy-shuffle: Customizable Fisher-Yates shuffle

Shuffles an array using the Fisher-Yates algorithm. 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).

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

Build Status

Coverage Status

Browser Support

Download