I am programming an C# Application, which should generate a random Password. My thought was to use the "Random" to generate 10 different Chars and combine them into 1 String.
So I made this Method, which returns a random Char:
public static string RandomChar()
{
Random random = new Random();
var chars ="$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&".ToCharArray();
string indexValue = chars.GetValue(random.Next(chars.Length)).ToString();
return indexValue;
}
This Method works as intended so far.
Then I made the following Method to generate a single string with 10 random chars:
public static string RandomPwd()
{
string Pwd = "";
for(int i = 0; i < 11; i++)
{
Pwd= Pwd + RandomChar();
}
return Pwd;
}
And then I simply call the RandomPwd() Method from on OnClick event. (I dont think this onClick Event is nessesary to show since the only thing it does there, is calling RandomPwd().)
The weird part is, that if I start the Application with an Breakpoint on
string a = RandomPwd();
"a" will always be a 10 Characters long string of SAME Charakters, like "UUUUUUUUUU".
But If i set the Breakpoint on the following:
for(int i = 0; i < 11; i++)
{
Pwd= Pwd + RandomChar();
}
return Pwd;
Then I can see, that Pwd is getting filled with 10 DIFFERENT Characters. And if I continue stepping through, I also notice, that
string a = RandomwPwd();
is now 10 Characters long of different Chars.
So it looks like the application works fine, as long as i step through manually, but if I dont, then it wont take different Randoms.
I´ve searched for that Problem on Google and on here, but I couldnt find anything which solves my Problem.
Maybe someone else has experienced that before and would share his Solution to this.
Thanks a lot!