Skip to content

Instantly share code, notes, and snippets.

@eric1234
Created June 15, 2010 20:09
Show Gist options
  • Select an option

  • Save eric1234/439654 to your computer and use it in GitHub Desktop.

Select an option

Save eric1234/439654 to your computer and use it in GitHub Desktop.
Random string generator
Array.prototype.random = function() {
return this[parseInt(Math.random() * this.length)];
}
String.random = function(len) {
var chars = $R('a', 'z').toArray().concat($R('A', 'Z').toArray());
return $R(1, len).inject('', function(m, i) {return m + chars.random()});
}
@eric1234
Copy link
Author

Ideal for generating random passwords. Just call:

String.random(8)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment