I have a loop in my code
Parallel.For(0, Cnts.MosqPopulation, i => { DoWork() });
however in the DoWork()
function, there are multiple calls to a random number generator which is defined as follows:
public static class Utils
{
public static readonly Random random = new Random();
}
It's static instance so that it is only seeded once. And I can use it throughout the code.
According to MSDN and other stackoverflow threads, this is not threadsafe. Infact, I have noticed at sometimes my code breaks and the random number generator starts generating all zeros (as per the MSDN documentation).
There are other stackoverflow threads, but are rather old and the implementation is slow. I can't afford to lose time on generating the numbers as the program is a scientific computation and is running hundreds of simulations.
I havn't worked with .net since the 2.0 days and I am not sure how the language has evolved to be able to make a fast, efficient, thread-safe RNG.
Here are the previous threads:
Is C# Random Number Generator thread safe?
Correct way to use Random in multithread application
Fast thread-safe random number generator for C#
Note: because I need a fast implementation, I can not use the RNGCryptoServiceProvider
which is rather slow.
Note2: I don't have a minimal working code. I don't even know where to begin as I don't know how thread-safety works or have high level knowledge of c#. So it does seem like I am asking for a complete solution.
I would consider something like this:
I'm not a huge fan of
ThreadStatic
these days. We have better tools than that usingThreadLocal
. Just use_random.Value
in your parallel loop and it will give you a newRandom
per thread.It combines an atomically incrementing value as well as the default behavior of using
Environemnt.TickCount
. The incrementing value is there to solve the problem of two Random's getting the same seed. Note that this approach only allows 255 randoms to be created. If you need more, then change the size of the mask.As you already noted, this isn't usable for secure purposes.
Using the
ThreadStatic
attribute and a custom getter, you will get a singleRandom
instance per thread. If this is not acceptable, use locks.The
ThreadStatic
attribute does not run the initializer on each thread so you are responsible for doing so in your accessor. Also think about your seed initializer, you can use something likeYou can inherit from
Random
to build a thread safe random classand use an instance of that class
If you have knowledge of how many threads you are running in parallel this may work:
Not sure how indistinguishable the resulting distribution would be from a uniform one though.