Last active
December 10, 2015 00:28
-
-
Save toruta39/4351117 to your computer and use it in GitHub Desktop.
Randomly sort an array
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
function randomsort(arr) { | |
var i = arr.length, j, tempi, tempj; | |
if ( i == 0 ) return this; | |
while ( --i ) { | |
j = Math.floor( Math.random() * ( i + 1 ) ); | |
tempi = arr[i]; | |
tempj = arr[j]; | |
arr[i] = tempj; | |
arr[j] = tempi; | |
} | |
return arr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The former version doesn't shuffle much of the array.