title: C# Random Number Generator getting stuck in a cycle
body: <p>I am using .NET to create an artificial life program and I am using C#'s pseudo random class defined in a Singleton. The idea is that if I use the same random number generator throughout the application, I could merely save the seed and then reload from the seed to recompute a certain interesting run.</p>
  
  <pre><code>public sealed class RandomNumberGenerator : Random
  {
      private static readonly RandomNumberGenerator instance = new RandomNumberGenerator();
  
      RandomNumberGenerator()
      {
  
      }
  
      public static RandomNumberGenerator Instance
      {
          get
          {
              return instance;
          }
      }
  }
  </code></pre>
  
  <p>I also wanted a method that could give me two different random numbers.</p>
  
  <pre><code>public static Tuple&lt;int, int&gt; TwoDifferentRandomNumbers(this Random rnd, int minValue, int maxValue)
      {
          if (minValue &gt;= maxValue)
              throw new ArgumentOutOfRangeException("maxValue", "maxValue must be greater than minValue");
          if (minValue + 1 == maxValue)
              return Tuple.Create&lt;int, int&gt;(minValue, maxValue);
  
          int rnd1 = rnd.Next(minValue, maxValue);
          int rnd2 = rnd.Next(minValue, maxValue);
          while (rnd1 == rnd2)
          {                
              rnd2 = rnd.Next(minValue, maxValue);
          }
          return Tuple.Create&lt;int, int&gt;(rnd1, rnd2);            
      }
  </code></pre>
  
  <p>The problem is that sometimes <code>rnd.Next(minValue,maxValue</code>always returns <code>minValue</code>. If I breakpoint at this point and try creating a double and setting it to <code>rnd.NextDouble()</code>, it returns 0.0. Anyone know why this is happening?</p>
  
  <p>I know that it is a pseudo random number generator, but frankly, I hadn't expected it to lock at 0. The random number generator is being accessed from multiple threads... could this be the source of the problem?</p>
  
  <p>EDIT : Thanks, the problem ended up being thread safety.</p>
  
  <p>This is the new version of the class.</p>
  
  <pre><code> public sealed class RandomNumberGenerator : Random
  {
      private static Random _global = new Random();
      [ThreadStatic]
      private static Random _localInstance;
  
      RandomNumberGenerator()
      {
  
      }
  
      public static Random Instance
      {
          get
          {
              Random inst = _localInstance;
              if (inst == null)
              {
                  int seed;
                  lock (_global) seed = _global.Next();
                  _localInstance = inst = new Random(seed);
              }
              return _localInstance;
          }
      }
  }
  </code></pre>