numOfNumericChars (Indicates number of numeric characters);
numOfAlphaChars (Indicates number of alpha characters);
numOfSynbolChars ( Indicates number of symbol characters)
validNumChars (Valid characters for numeric. Default is all characters from 0- 9)
validAlphaChars (Valid characters for alpha. Default is all characters from a- z and A-Z)
validSymbolChars (Valid character for symbols. Default is all characters in !-+_@&$#%)
C#
public string generatePassword(int numOfNumericChars, int numOfAlphaChars, int numOfSynbolChars, string validNumChars = "1234567890", string validAlphaChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", string validSymbolChars = "!-+_@&$#%")
{
StringBuilder res = new StringBuilder();
Random rnd = new Random();
while (0 < numOfNumericChars--)
{
res.Append(validNumChars[rnd.Next(validNumChars.Length)]);
}
while (0 < numOfAlphaChars--)
{
res.Append(validAlphaChars[rnd.Next(validAlphaChars.Length)]);
}
while (0 < numOfSynbolChars--)
{
res.Append(validSymbolChars[rnd.Next(validSymbolChars.Length)]);
}
string stgPassword = res.ToString();
//Shuffle password
char[] array = stgPassword.ToCharArray();
Random rng = new Random();
int n = array.Length;
while (n > 1)
{
n--;
int k = rng.Next(n + 1);
var value = array[k];
array[k] = array[n];
array[n] = value;
}
return stgPassword = new string(array);
}