Skip to content

Instantly share code, notes, and snippets.

@toruta39
Last active December 10, 2015 00:28
Show Gist options
  • Save toruta39/4351117 to your computer and use it in GitHub Desktop.
Save toruta39/4351117 to your computer and use it in GitHub Desktop.
Randomly sort an array
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;
}
@toruta39
Copy link
Author

The former version doesn't shuffle much of the array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment