I'm experiencing a problem with my random number generator in a class library returning the same value repeatedly. It returns the same value b/c it is repeatedly initialized with the default constructor - for example:
public static T GetRandomValue<T>(T[] array)
{
int ndx = new Random().Next(array.Length);
return array[ndx];
}
This is called repeatedly from another method before the system clock can change, so it is initialized with the same random seed, giving the same value. (see SO article for apparently malfunctioning random number generator) It is used to select a random format string for some text generation algorithms. Because it is called in rapid sequence every time my different bits of text generates with a homogeneous formatting string, which is undesirable for the application.
It is typically called from an asp.net webpage, and I'm wondering what the best approach is to produce a random sequence, without creating performance issues for the pages that call the method repeatedly.
A web page calls the library method, which calls the random number. I am wondering if i can use this approach for a static number generator instead. Are there performance issues associated with calling a static method like this from a webpage?
public class Utility
{
public static Random random = new Random();
public static T GetRandomValue<T>(T[] array)
{
int ndx = random.Next(array.Length);
return array[ndx];
}
}
A lock on "random" and "ndx" may be needed too. Is there a better practice in general for handling this type of seeding in a class library?