compute-shuffle

1.0.0 • Public • Published

shuffle

NPM version Build Status Coverage Status Dependencies

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.

Installation

$ npm install compute-shuffle

For use in the browser, use browserify.

Usage

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 ] );

Examples

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

Notes

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.

Tests

Unit

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

License

MIT license.


Copyright

Copyright © 2014. Athan Reines.

Package Sidebar

Install

npm i compute-shuffle

Weekly Downloads

68

Version

1.0.0

License

none

Last publish

Collaborators

  • kgryte