Skip to content

Instantly share code, notes, and snippets.

@FergusInLondon
Last active April 14, 2017 22:47
Show Gist options
  • Save FergusInLondon/05663ca189046017145500ab2a1e4aeb to your computer and use it in GitHub Desktop.
Save FergusInLondon/05663ca189046017145500ab2a1e4aeb to your computer and use it in GitHub Desktop.
Simple array shuffler example.
/**
* shuffle(array[]) - Shuffles an array via a simplistic
* algo, not too dissimilar to Fisher-Yates.
*
* Ignore the -ahem- "clever" variable swapping one-liner,
* that's all it does.
*
* @param array
* @return array
*/
var shuffle = function(list) {
var rand = 0, len = list.length;
for( var i = 0; i < len; i++ ) {
// rand must be: (i < rand < len)
rand = Math.floor(Math.random() * (len - i)) + i;
[list[rand], list[i]] = [list[i], list[rand]]
}
return list;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment