Skip to content

Instantly share code, notes, and snippets.

@fliptopbox
Last active August 25, 2015 21:37
Show Gist options
  • Save fliptopbox/ed8b515d39ef846041dd to your computer and use it in GitHub Desktop.
Save fliptopbox/ed8b515d39ef846041dd to your computer and use it in GitHub Desktop.
Javascript Array.shuffleAndClip()
Array.prototype.shuffleAndClip = function (len) {
var array = this;
if(len && array.length && len > array.length - 1) {
len = null;
}
for(var rnd, value, i = array.length; i;
rnd = Math.floor(Math.random() * i),
value = array[--i],
array[i] = array[rnd],
array[rnd] = value);
return len ? array.slice(0, len) : array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment