Skip to content

Instantly share code, notes, and snippets.

@vst
Created June 30, 2014 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vst/999bef66a2949497e034 to your computer and use it in GitHub Desktop.
Save vst/999bef66a2949497e034 to your computer and use it in GitHub Desktop.
Javascript function which produces random strings
/**
* Constructs a random string with the given length and character set.
*
* If no character set is provided, default character set will be used.
*/
function randstr (length, charset) {
// (Re)define the charset:
charset = charset || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
// construct and return:
return Array.apply(null, Array(length)).map(function (x) {
return charset[Math.floor(Math.random() * charset.length)];
}).join("");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment