Help – Console output not displaying correctly

It's been awhile since I've done any programming and I've started to need it a little for work now, so I've decided to start going through some beginner exercises to refresh my brain. Anyways, this is a quick little random password generator program that I wrote as part of it. When I run the program and tell it to generate multiple passwords, it displays the same password for each of them. However, if I put a breakpoint in the program and step through it, I can see that it generates a different password each time and even displays each password correctly in the console. WTF is going on? am I missing something simple?

 static void Main(string[] args) { int length, number; string output; Console.Write("Enter length of password to generate: "); length = int.Parse(Console.ReadLine()); Console.Write("Enter number of passwords to generate: "); number = int.Parse(Console.ReadLine()); for (int i = 0; i < number; i++) { output = GeneratePassword(length); Console.WriteLine(output); } Console.ReadLine(); } static string GeneratePassword(int length) { string password = ""; Random r = new Random(); for (int i = 0; i < length; i++) { password += (char)r.Next(33, 126); } return password; } 

by szymanc via /r/csharp

Leave a Reply