views:

175

answers:

5

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!

+12  A: 

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...

Jon Skeet
Thanks, this is exactly what I needed!
Mandelbrot
+4  A: 

I don't believe that the Random class is designed to be thread safe (concurrently usable from multiple threads) - so if you're sharing a single instance in this manner, you may corrupt the state of the random generator, preventing it from operating correctly.

You can decorate the static variable that holds the reference to the Random class as ThreadStatic, which will allow you to maintain a separate instance per thread:

[ThreadStatic]
private static Random m_Random;  // don't attempt to initialize this here...

public void YourThreadStartMethod()
{
    // initialize each random instance as each thread starts...
    m_Random = new Random();
}

If you're using .NET 4.0, there's also the ThreadLocal<T> class, which helps make initializing one instance per thread easier.

LBushkin
+1  A: 

This is a duplicate of this question I think:

http://stackoverflow.com/questions/295900/system-random-keeps-on-returning-the-same-value

FinnNk
A: 

I think you should instead use the RNGCryptoServiceProvider

http://msdn.microsoft.com/en-us/library/system.security.cryptography.rngcryptoserviceprovider.aspx

Fosco
+1  A: 

The Random object is not thread safe. To get around that, you could use this code stolen from this answer:

class ThreadSafeRandom 
{ 
    private static Random random = new Random(); 

    public static int Next() 
    { 
       lock (random) 
       { 
           return random.Next(); 
       } 
    } 
}

You could also use the RNGCryptoServiceProvider, which is thread safe and also produces better random data.

josh3736