I have a method below for randomly generating alphanumeric combinations. If I call it in a loop it returns the same combination but if I place a break point on GetVoucherNumber
and step through it a different number is generated every time. Could you please explain why this happens and how to avoid it?
Code:
static void Main(string[] args)
{
for (int i = 0; i < 100; i++)
Console.WriteLine(GetVoucherNumber(6));
Console.ReadLine();
}
public static string GetVoucherNumber(int length)
{
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var random = new Random();
var result = new string(
Enumerable.Repeat(chars, length)
.Select(s => s[random.Next(s.Length)])
.ToArray());
return result;
}