The Fisher-Yates (aka Knuth) shuffle for Browser JavaScript and Node.js
Go to file
AJ ONeal e23b582738 Update README.md 2016-11-25 10:33:10 -07:00
.gitignore Initial commit 2013-12-02 23:37:01 -08:00
.jshintrc every js project deserves a jshint 2013-12-03 00:45:50 -07:00
LICENSE Initial commit 2013-12-02 23:37:01 -08:00
README.md Update README.md 2016-11-25 10:33:10 -07:00
bower.json added bower.json 2013-12-03 13:20:38 -07:00
example.js initial commit 2013-12-03 00:44:42 -07:00
index.js sniff global as window or global due to new CSP in browser 2014-09-28 02:22:08 -07:00
package.json sniff global as window or global due to new CSP in browser 2014-09-28 02:22:08 -07:00

README.md

Daplie is Taking Back the Internet!

Stop serving the empire and join the rebel alliance!

knuth-shuffle

The Fisher-Yates (aka Knuth) shuffle for Browser and Node.js

'nuf said.

The Fisher-Yates (Knuth) Shuffle

As Microsoft learned the hard way (see article below), function random() { return 0.5 - Math.random() } turns out to be no-so-random at all.

The fisher-yates shuffle is an algorithm so simple that even IEEE floating point math can't screw it up!

I put this on npm as knuth-shuffle because fisher-yates-shuffle was just too long of a name and shuffle was already taken.

Browser Example

<script src="https://raw.github.com/coolaj86/knuth-shuffle/master/index.js"></script>
(function () {
  'use strict';

  var a = [2,11,37,42]
    , b
    ;

  // The shuffle modifies the original array
  // calling a.slice(0) creates a copy, which is assigned to b
  b = window.knuthShuffle(a.slice(0));
  console.log(b);
}());

Node Example

npm install -S knuth-shuffle
(function () {
  'use strict';

  var shuffle = require('knuth-shuffle').knuthShuffle
    , a = [2,11,37,42]
    , b
    ;

  // The shuffle modifies the original array
  // calling a.slice(0) creates a copy, which is assigned to b
  b = shuffle(a.slice(0));
  console.log(b);
}());