I am trying to create a list of randomly generated passwords in a WinForms application. In the below code, if I put a breakpoint on the Console.WriteLine(""); line, the specified amount of different passwords are generated with the specified chars. If I do not place a breakpoint here, all of the passwords returned are the same.
I know I am probably missing something very simple, but can't see it. There are no threads, this is happening on the UI thread at this point. I have done some testing with test console and WinForms applications, same result - so I don't think it is a WinForms related issue. Any ideas?
class PasswordGenerator
{
public List<String> GenerateLowSecurityPasswords(int numOfChars, int numOfPasswords)
{
List<String> passwords = new List<String>();
for (int i = 0; i < numOfPasswords; i++)
{
passwords.Add(this.GenerateLowSecurityPassword(numOfChars));
Console.WriteLine("");
}
return passwords;
}
private String GenerateLowSecurityPassword(int numOfChars)
{
Random rnd = new Random();
char[] passwd = new char[numOfChars];
string allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
for (int i = 0; i < numOfChars; i++)
{
passwd[i] = allowedChars[rnd.Next(0, allowedChars.Length)];
}
return new String(passwd);
}
}