| 
									
										
										
										
											2013-12-02 23:37:01 -08:00
										 |  |  |  | knuth-shuffle | 
					
						
							|  |  |  |  | ============= | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | The Fisher-Yates (aka Knuth) shuffle for Browser and Node.js | 
					
						
							| 
									
										
										
										
											2013-12-03 01:37:35 -06:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:50:09 -06:00
										 |  |  |  |   * [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. | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:37:35 -06:00
										 |  |  |  | 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. | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:50:09 -06:00
										 |  |  |  | The fisher-yates shuffle is an algorithm so simple that even | 
					
						
							| 
									
										
										
										
											2013-12-03 01:37:35 -06:00
										 |  |  |  | [IEEE floating point math](http://blogs.adobe.com/bparadie/2011/11/22/0-2-0-1-0-30000000000000004/) | 
					
						
							| 
									
										
										
										
											2013-12-03 01:50:09 -06:00
										 |  |  |  | can't screw it up! | 
					
						
							| 
									
										
										
										
											2013-12-03 01:37:35 -06:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 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 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:47:42 -06:00
										 |  |  |  | <script src="https://raw.github.com/coolaj86/knuth-shuffle/master/index.js"></script> | 
					
						
							| 
									
										
										
										
											2013-12-03 01:37:35 -06:00
										 |  |  |  | ``` | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | ```javascript | 
					
						
							|  |  |  |  | (function () { | 
					
						
							|  |  |  |  |   'use strict'; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 03:43:42 +00:00
										 |  |  |  |   var a = [2,11,37,42]; | 
					
						
							|  |  |  |  |   var b; | 
					
						
							| 
									
										
										
										
											2013-12-03 01:37:35 -06:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   // The shuffle modifies the original array | 
					
						
							|  |  |  |  |   // calling a.slice(0) creates a copy, which is assigned to b | 
					
						
							|  |  |  |  |   b = window.knuthShuffle(a.slice(0)); | 
					
						
							| 
									
										
										
										
											2013-12-03 01:38:21 -06:00
										 |  |  |  |   console.log(b); | 
					
						
							| 
									
										
										
										
											2013-12-03 01:44:22 -06:00
										 |  |  |  | }()); | 
					
						
							| 
									
										
										
										
											2013-12-03 01:37:35 -06:00
										 |  |  |  | ``` | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | Node Example | 
					
						
							|  |  |  |  | === | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-28 21:51:44 -06:00
										 |  |  |  | Decentralized Install | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | ```bash | 
					
						
							|  |  |  |  | npm install --save git+https://git.coolaj86.com/coolaj86/knuth-shuffle.js.git#v1.0 | 
					
						
							|  |  |  |  | ``` | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | Centralized Install | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:37:35 -06:00
										 |  |  |  | ```bash | 
					
						
							| 
									
										
										
										
											2017-10-28 21:51:44 -06:00
										 |  |  |  | npm install --save knuth-shuffle@1.0 | 
					
						
							| 
									
										
										
										
											2013-12-03 01:37:35 -06:00
										 |  |  |  | ``` | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | ```javascript | 
					
						
							|  |  |  |  | (function () { | 
					
						
							|  |  |  |  |   'use strict'; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 03:43:42 +00:00
										 |  |  |  |   var shuffle = require('knuth-shuffle').knuthShuffle; | 
					
						
							|  |  |  |  |   var a = [2,11,37,42]; | 
					
						
							|  |  |  |  |   var b; | 
					
						
							| 
									
										
										
										
											2013-12-03 01:37:35 -06:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   // The shuffle modifies the original array | 
					
						
							|  |  |  |  |   // calling a.slice(0) creates a copy, which is assigned to b | 
					
						
							|  |  |  |  |   b = shuffle(a.slice(0)); | 
					
						
							| 
									
										
										
										
											2013-12-03 01:38:21 -06:00
										 |  |  |  |   console.log(b); | 
					
						
							| 
									
										
										
										
											2013-12-03 01:44:22 -06:00
										 |  |  |  | }()); | 
					
						
							| 
									
										
										
										
											2013-12-03 01:37:35 -06:00
										 |  |  |  | ``` |