- public static string GeneratePassword(int minLen, int maxLen, bool isUpperCase = true, bool isLowerCase = true, bool isNumber = true, bool isSymbol = true)
- {
- string allowedChars = string.Empty;
- if (isUpperCase)
- allowedChars += "ABCDEFGHIJKLMNOPQRSTUVWXTZ";
- if (isLowerCase)
- allowedChars += "abcdefghiklmnopqrstuvwxyz";
- if (isNumber)
- allowedChars += "1234567890";
- if(isSymbol)
- allowedChars += "!'^#+$%&/{([)]=}*?\\_-@~,;:<>";
- int len = rd.Next(minLen, maxLen);
- int randomIndex;
- char randomChar;
- for (int i = 0; i <= len; i++)
- {
- randomIndex = rd.Next(allowedChars.Length);
- randomChar = allowedChars[randomIndex];
- sb.Append(randomChar);
- }
- return sb.ToString();
- }
Here is the result for min length = 5 and max length = 15
rsN\&,Juk
~1-?Q'~Mku
wFnElGP:L+8sA
FN#'(i
Q6oid5l
xe$iFlD
ft*kA;
)u-)Kx
o3L2fn>9
o%=2i-
~1-?Q'~Mku
wFnElGP:L+8sA
FN#'(i
Q6oid5l
xe$iFlD
ft*kA;
)u-)Kx
o3L2fn>9
o%=2i-
Random object use seed from system clock. If you are going to create passwords consecutive in very small time interval, probably random.next will return same value. In this case define random object as a static field. Also you can pass it as parameter. Even you can seed random object by yourself but you must change seed value everytime you call. You can use a counter.
- Random rd = new Random(seed);