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.