Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Random strings generator
function randomString(length,numNumeric,numLCase,numUCase,numSymbols){
length=length||16;numNumeric=numNumeric||3,numLCase=numLCase||3,numUCase=numUCase||3,numSymbols=numSymbols||3;
var charsList = [
'0123456789',
'abcdefghijklmnopqrstuvwxyz',
'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'!"#$%&\'()*+,-./\\:;<=>?@[]^_`{|}~'
],
charLength = [numNumeric,numLCase,numUCase,numSymbols],
str="",i=0;
while(i<length){
skipchars=true,j=0;
while(skipchars&&j<4){
charsType=Math.floor(Math.random()*charsList.length);
numCharsCount = charLength[charsType];
if(numCharsCount > 0) skipchars=false;
j++;
}
if(j>=4)
charsType=Math.floor(Math.random()*(charsList.length-1));
str+=charsList[charsType][Math.floor(Math.random()*charsList[charsType].length)];
charLength[charsType]--;i++;
}
return str;
}
@anon5r
Copy link
Author

anon5r commented Nov 27, 2014

Reinvent the wheel.

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