Shuffles array elements in place.

Downloads in past


411.0.09 years ago9 years agoMinified + gzip package size for compute-shuffle in KB


!NPM versionnpm-imagenpm-url !Build Statustravis-imagetravis-url !Coverage Statuscoveralls-imagecoveralls-url !Dependenciesdependencies-imagedependencies-url
Shuffles array elements in place.

This module implements the Fisher-Yates shuffle (a.k.a. the Knuth shuffle) to generate a random permutation of a finite set of array elements.


$ npm install compute-shuffle

For use in the browser, use browserify.


To use the module,
var shuffle = require( 'compute-shuffle' );

shuffle( arr )

Mutates the input array to generate a random permutation of array elements.
shuffle( [ 1, 2, 3 ] );


var shuffle = require( 'compute-shuffle' );

var data = new Array( 20 );
for ( var i = 0; i < data.length; i++ ) {
	data[ i ] = i;

var copy;
for ( var j = 0; j < 10; j++ ) {
	copy = data.slice();
	shuffle( copy );
	console.log( copy );

To run the example code from the top-level application directory,
$ node ./examples/index.js


Beware of implementations on NPM and elsewhere which use bitwise operators to floor numeric values. When using bitwise operators, numeric values are converted to 32-bit integers. For large numeric values, you will experience unexpected results. While not a concern for this algorithm, bitwise operators also produce unexpected results when used on negative numeric values.



Unit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:
$ make test

All new feature development should have corresponding unit tests to validate correct functionality.

Test Coverage

This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:
$ make test-cov

Istanbul creates a ./reports/coverage directory. To access an HTML version of the report,
$ make view-cov


MIT license.


Copyright © 2014. Athan Reines.