Created
April 27, 2015 12:21
-
-
Save Pushplaybang/0aa1ec5645d67933b045 to your computer and use it in GitHub Desktop.
Fischer Yates Shuffle in Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//as a function | |
function shuffleFischerYates(arr) { | |
var i = arr.length, j, temp; | |
while (--i > 0) { | |
j = Math.floor(Math.random()*(i+1)); | |
temp = arr[j]; | |
arr[j] = arr[i]; | |
arr[i] = temp; | |
} | |
} | |
// or extending a prototype | |
Array.prototype.shuffle = function() { | |
var = this.length, j, temp; | |
while (--i > 0) { | |
j = Math.floor(Math.random()*(i+1)); | |
temp = arr[j]; | |
arr[j] = arr[i]; | |
arr[i] = temp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment