Last active
August 25, 2015 21:37
-
-
Save fliptopbox/ed8b515d39ef846041dd to your computer and use it in GitHub Desktop.
Javascript Array.shuffleAndClip()
This file contains hidden or 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
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