Created
June 30, 2014 09:49
-
-
Save vst/999bef66a2949497e034 to your computer and use it in GitHub Desktop.
Javascript function which produces random strings
This file contains 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
/** | |
* 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