I have a static method in a static class which can generate random strings, like this:
public static class DataGenerator
{
public static string GenerateRandomString(int length)
{
const string Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var random = new Random();
return new string(
Enumerable.Repeat(Chars, length)
.Select(s => s[random.Next(s.Length)])
.ToArray());
}
}
When I call this method multiple times from the same calling function, it seems to cache the generated string.
Here is an example of usage:
var item = new CodeDescActivePo()
{
Active = true,
Code = DataGenerator.GenerateRandomString(10),
Description = DataGenerator.GenerateRandomString(10)
};
Notice that there are 2 calls to GenerateRandomString, I would expect 2 unique random numbers, in this case Code and Description are always the same.
Why would GenerateRandomString not generate a fresh random number each time?