So I'm a c# noob. I have a quick question that I can't find an answer to anywhere else.
[Serializable()]
public class Dictionary
{
private Random m_RandomGenerator = new Random();
public int GetNext()
{
return m_RandomGenerator.Next(100);
}
}
The Dictionary
instance is loaded each time the program starts, and this code will return the exact same sequence of numbers each time it is run. By which I mean, each time the executable is run. Surely the time value it's seeded with should be different (DateTime.Now.Ticks I assume?).
A couple of points:
- There is only one instance of Dictionary, deserialized from a previously-exported file at startup.
- If I make m_RandomGenerator static then the problem is fixed.
Does anyone know why? I did double check that I'm not creating a new instance of Dictionary each time, so that's not the issue.
Well, colour me embarrassed. As it turns out the culprit was the [Serializable()] attribute. The dictionary class I was using was loaded from a previously exported file, which was obviously loading the seed back into Random(). Changing the variable to static meant that the seed value was no longer loaded from the previously serialised instance - hiding the issue.
Thanks to all the people offering constructive advice!