1.0.8 • Published 8 years ago

knuth-shuffle v1.0.8

Weekly downloads
8,650
License
(MIT OR Apache-2....
Repository
-
Last release
8 years ago

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

Decentralized Install

npm install --save git+https://git.coolaj86.com/coolaj86/knuth-shuffle.js.git#v1.0

Centralized Install

npm install --save knuth-shuffle@1.0
(function () {
  'use strict';

  var shuffle = require('knuth-shuffle').knuthShuffle;
  var a = [2,11,37,42];
  var 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);
}());
storj-bridge@deadcanaries/kadencelorem-marxumpoker-holdem-engine@orcproject/orcsynchronized-array-shuffletarot-decksyniweirdstorj-libisolation-forestkad-quasarkadence-kimhuman-readable-idshubot-gameslove-languageloudbot-slackmeetup-randomizer@withyouwithme/b5-questions@util.js/arraysallbadcards-human-readable-idsb5-johnson-120-ipip-neo-pi-rstrilityutiljs-arraysxopolymetascript-cspnatural-selection-modelsd-zookeeperprogress-a-historyset-gamenode-as-a-so-thatneural_networknode-scientistpull-reviewregexgeneratorrandomrowproxy-rotatorgoogle.js@flood/element-core@pointnetwork/kadencefs-job-storegenaro-lib@szegerege12/b5-johnson-120-ipip-neo-pi-r-pl@tdjsnelling/isolation-forestetcd-sdfind-npmflashcards@blueabel/b5-50@blueabel/b5-costa-mccrae-300@blueabel/b5-johnson-120@blueabel/e1-potgieter-60@ceejbot/tarot@botpoker/engine-holdemcard-sharingcard-sharpcascading-grid-pageletchinese-whisperscli-flashcardsclan-quizcreedeck-jsblackjack-simulatorblackjack-toolsbig-human-readable-idsbetterlyearn2boxpusherbigfivequestionscustom-2020dnd-human-readable-ids@adam.biltcliffe/codex@ancepsinfans/b5-johnson-120-ipip-neo-pi-r@alheimsins/b5-50-ipip-neo-pi-r@alheimsins/b5-costa-mccrae-300-ipip-neo-pi-r@alheimsins/b5-johnson-120-ipip-neo-pi-r@sjx233/luogu-painter@quainetwork/quai-snap@kadenceproject/kadence@rx-irc/bot-quiz@rpggenerator/rpggenerator-citygen-data-conversion@rpggenerator/rpggenerator-citygen-random@rpggenerator/rpggenerator-citygen-tables@mobylogix/b5-johnson-120-ipip-neo-pi-r@sbr-technologies/b5-johnson-120-ipip-neo-pi-r
1.0.8

8 years ago

1.0.7

8 years ago

1.0.6

8 years ago

1.0.5

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

11 years ago

1.0.0

12 years ago