Created
January 30, 2013 08:34
-
-
Save carloscabo/4671669 to your computer and use it in GitHub Desktop.
#js #random #shuffle #array #fisher #yates Shuffles array's elements
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
var fisherYates = function( myArray ) { | |
var i = myArray.length, j, tempi, tempj; | |
if ( i == 0 ) return false; | |
while ( --i ) { | |
j = Math.floor( Math.random() * ( i + 1 ) ); | |
tempi = myArray[i]; | |
tempj = myArray[j]; | |
myArray[i] = tempj; | |
myArray[j] = tempi; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment