Skip to content

Instantly share code, notes, and snippets.

@anon5r
Created November 27, 2014 03:10
Show Gist options
  • Select an option

  • Save anon5r/d2edc78b49ae5ee92e53 to your computer and use it in GitHub Desktop.

Select an option

Save anon5r/d2edc78b49ae5ee92e53 to your computer and use it in GitHub Desktop.
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
Copy Markdown
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