Skip to content

Instantly share code, notes, and snippets.

@cange
Created August 5, 2010 09:44
Show Gist options
  • Save cange/509487 to your computer and use it in GitHub Desktop.
Save cange/509487 to your computer and use it in GitHub Desktop.
Generate unique random alphanumeric strings #learnjs
/**
* #learnjs
* Generate unique random alphanumeric strings
* @return {int} length
*/
function passGen(length) {
var str = '',
randomChar = function () {
var num = Math.floor(Math.random() * 62),
charCodeNum = (num < 36) ? num + 55 : num + 61;
return (num < 10 ? num : String.fromCharCode(charCodeNum));
};
while (str.length < length) {
str += randomChar();
}
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment