Skip to content

Instantly share code, notes, and snippets.

@foldi
Created March 31, 2013 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foldi/5281034 to your computer and use it in GitHub Desktop.
Save foldi/5281034 to your computer and use it in GitHub Desktop.
/**
* Randomly reorders the contents of an array in place.
* @author blixt (https://github.com/blixt)
* @param {Array.<Object>} arr An array.
* @returns {Array.<Object>} The same array that was passed in,
* now shuffled.
*/
function shuffleArray(arr) {
var i, j, item;
for (i = arr.length - 1; i > 0; i--) {
j = Math.round(Math.random() * i);
item = arr[i];
arr[i] = arr[j];
arr[j] = item;
}
return arr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment