The following method generates a random string with the given length.
When executed twice in a row, the same string is given. Is anybody able to shed any light on why this might be?
public static string GenerateRandomString(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
Random random = new Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
You are not using
Randomcorrectly.When calling your
GenerateRandomStringin a tight loop, it will end up being seeded to the same value many times.I suggest reading Random numbers from the C# in depth site, by Jon Skeet.
Here is the suggested way to get a
Randominstance: