Created
July 2, 2012 05:14
-
-
Save thenoviceoof/3031220 to your computer and use it in GitHub Desktop.
Fisher-Yates shuffle in js
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
// using http://en.wikipedia.org/wiki/Fisher-Yates_shuffle | |
function randrange(i) { | |
return Math.floor(i*Math.random()); | |
} | |
// in-place shuffle | |
function shuffle(items) { | |
var tmp; | |
var i = items.length - 1; | |
while(i >= 1) { | |
j = randrange(i); | |
tmp = items[i]; | |
items[i] = items[j]; | |
items[j] = tmp; | |
i -= 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment