Created
August 5, 2010 09:44
-
-
Save cange/509487 to your computer and use it in GitHub Desktop.
Generate unique random alphanumeric strings #learnjs
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
/** | |
* #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