# knuth-shuffle

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

Latest version **1.0.8** (published 2017-10-29) · (MIT OR Apache-2.0) license · 0 weekly downloads

## Install

```sh
npm install knuth-shuffle
pnpm add knuth-shuffle
yarn add knuth-shuffle
bun add knuth-shuffle
```

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.8 |
| Published | 2017-10-29 |
| First published | 2013-12-03 |
| Weekly downloads | 0 |
| License | (MIT OR Apache-2.0) |
| TypeScript types | separate (@types/knuth-shuffle) |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | AJ ONeal |
| Maintainers | coolaj86, creationix, daplieinc, drewwarrentiy, insightfuls, jimh, tigerbot |
| Keywords | ronald, fisher, frank, yates, fisher-yates, donald, knuth, shuffle, random, randomize, unbiased, algorithm |

## Links

- npm: https://www.npmjs.com/package/knuth-shuffle
- Repository: https://git.coolaj86.com:coolaj86/knuth-shuffle.js
- Homepage: https://git.coolaj86.com/coolaj86/knuth-shuffle.js
- Issues: https://git.coolaj86.com/coolaj86/knuth-shuffle/issues
- npm.io page: https://npm.io/package/knuth-shuffle

## Recent versions

- 1.0.8 (latest) — 2017-10-29
- 1.0.1 — 2014-09-28
- 1.0.0 — 2013-12-03

## README

knuth-shuffle
=============

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

  * [Mike Bostock's Fisher–Yates Shuffle Visualization](http://bost.ocks.org/mike/shuffle/)
  * [How to randomize/shuffle a JavaScript array](http://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array)
  * [Fisher-Yates Shuffle on Wikipedia](http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle)
  * [Doing the Microsoft Shuffle: Algorithm Fail in Browser Ballot](http://www.robweir.com/blog/2010/02/microsoft-random-browser-ballot.html)
  * [knuth-shuffle on NPM](https://npmjs.org/package/knuth-shuffle)

'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](http://blogs.adobe.com/bparadie/2011/11/22/0-2-0-1-0-30000000000000004/)
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
===

```html
<script src="https://raw.github.com/coolaj86/knuth-shuffle/master/index.js"></script>
```

```javascript
(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

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

Centralized Install

```bash
npm install --save knuth-shuffle@1.0
```

```javascript
(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);
}());
```

---
_Source: https://npm.io/package/knuth-shuffle · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
