Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Random.Next suddenly stops returning random values [duplicate]

Possible Duplicate:
System.Random keeps on returning the same value

I'm refactoring and expanding a small C# agent-based model to help some biology professors predict the spread of a disease. Each year of the simulation each individual agent randomly travels to a nearby population node, possibly spreading the disease. I'm brand new to C#, but I've read about potential problems with Random.Next returning the same value if re-initialized with the same system time. To avoid this I've created a static instance which is referenced for each new random value.

The specifics:

In my efforts to scale up the model I've changed it to compute the "travel" information for each population node in parallel. When testing the model I noticed that in the new version the disease would not spread past the first year. Further inquiry narrowed the problem down to the travel between nodes. After the first year all the agents remained immobile. I examined the function responsible for their travel and found that it works by creating a list of all nearby nodes, generating a random number <= the number of elements in the list, and travelling to listOfNearbyNodes[myRandomNumber].

The problem:

I then added a print statement to output the value of the random index for each iteration. I found that the whole model works exactly as expected for the first year of the simulation, with the random numbers being generated in an acceptable range. However, after the first year ends and the simulation loops the exact same code will only return a "random" index of 0. Every thread, every iteration, every node, every agent, always 0. As the current node of an agent is always the first item in the list the agents never move again.

I thought this might be another manifestation of the system time seed error, so I've tried three different ways of implementing a static random object, but it doesn't help. Every time I run the simulation the first year always works correctly and then Random.Next() starts to only return 0's.

Does anyone have ideas as to where I should look next for the bug? Thanks!

like image 946
Mandelbrot Avatar asked Jul 07 '10 16:07

Mandelbrot


1 Answers

I suspect you're using the same instance of Random concurrently in multiple threads. Don't do that - it's not thread-safe.

Options:

  • Create a new instance of Random per thread (ThreadStatic can help here)
  • Use a single instance of Random, but only ever use it in a lock.

I have a blog post with some sample code, but please read the comments as well as there are good suggestions for improving it. I'm planning on writing another article about randomness at some point in the near-ish future...

like image 164
Jon Skeet Avatar answered Oct 13 '22 22:10

Jon Skeet