Hello
I'm coding on a simulator at the moment, what it is simulating is not very important. Each simulation returns a number in the 7000-10000 range, depending on certain random events. The simulation is run several thousand times to get a good average value.
The problem: after running around 1000 to 4500 simulations (it varies), all new random numbers are zero which makes all the following simulations identical. The current implementation looks like this:
in class Simulation:
public static Random Random = new Random();
.....
public double getRandom()
{
double random = Random.NextDouble();
Console.Write(String.Format("{0:0.00} ", random));
return random;
}
The getRandom method is called around 500 times per simulation, and several thousand simulations are run in 2 threads working in parallel. This problem didn't exist before i introduced multithreading in the program and I'm frankly stumped of how it can appear since Random is supposed to be thread safe when used as a static.
Any help would be greatly appreciated.
Loading
Rasmus FrankePosted Dec 4, 2009, 10:19 AM
Thanks for the help!
Sam HobbsPosted Dec 2, 2009, 11:51 AM
Assuming that a lock is not needed, the other possibility is memory corruption. I would look to see if there is anything that could be clobbering memory. I would look to see if there is debugging functions I could use to catch alteration of memory outside of specified sections of code. Simple data breakpoints would not help since the object would be properly modified during normal use of the object. Debugging memory corruption is among the most difficult bugs to diagnose and the easiest way, if it can work, is to look at the code. It might not be possible to find the bug that way but if it is possible then it is likely the easiest way.
Rasmus FrankePosted Dec 1, 2009, 6:27 PM
I did kind of solve the problem, but in an ugly way. I simple made a check if the random numbers were starting to become zero, and in that case replaced the Random with a new Random, giving it a new seed. It works, but I'm still very curious about what could make behavior such as this, I can't imagine any thread problem that could make a random suddenly start throwing out only zeros.
Sam HobbsPosted Dec 1, 2009, 6:17 PM
Are you sure that the Random class has built-in protection to prevent simultaneous access from multiple threads? I doubt it, but I don't know. It is typically our responsibility to do things such as that. If you are using the same object from multiple threads then I think you must provide the serialization to ensure the object is used by only one thread at a time. Do you know how that works?
Rasmus FrankePosted Dec 1, 2009, 12:06 PM
Sam HobbsPosted Dec 1, 2009, 11:49 AM
at the end of lines you will get a line break.
To answer your question, look at the Random Constructor documentation; it says "Random objects that are created in close succession by a call to the default constructor will have identical default seed values and, therefore, will produce identical sets of random numbers". Does that explain the results you are getting?