Skip to content

Instantly share code, notes, and snippets.

@pbojinov
Forked from spideron/String.prototype.shuffle
Last active April 8, 2018 02:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pbojinov/4733869 to your computer and use it in GitHub Desktop.
Save pbojinov/4733869 to your computer and use it in GitHub Desktop.
Shuffle string
/**
* Shuffle string like crazy
*
* @param {int} length desired
* @return {string} the shuffled string
*/
String.prototype.shuffle = function(size) {
var arr = this.split('');
for(var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x){};
arr = arr.join('');
return size ? arr.substr(0, Math.min(size, arr.length-1)) : arr;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment