1
Vote

DBPop random generator multi-threading issue

description

DBPop is using a non-thread safe implementation of the Random class that can result in it always returning 0.
 
http://blogs.msdn.com/brada/archive/2003/08/14/50226.aspx
 
Replacing the instances of m_randomInstance in the code to reference the following class will resolve this.
 
public class dbRandom
{
    private static Random m_randomInstance = new Random(Thread.CurrentThread.GetHashCode() + Environment.TickCount);
 
    // Lock is needed as Random isn't totally Thread safe
    public static int getNext()
    {
        lock (m_randomInstance)
        {
            return m_randomInstance.Next();
        }
    }
    public static int getNext(int maxVal)
    {
        lock (m_randomInstance)
        {
            return m_randomInstance.Next(maxVal);
        }
    }
    public static int getNext(int minVal, int maxVal)
    {
        lock (m_randomInstance)
        {
            return m_randomInstance.Next(minVal, maxVal);
        }
    }
}

comments