title: Why does an instance of Random in a serialized class repeatedly produce the same set of numbers?
body: <p>So I'm a c# noob. I have a quick question that I can't find an answer to anywhere else.</p>
  
  <pre><code> [Serializable()]
   public class Dictionary
   {
      private Random m_RandomGenerator = new Random();
  
      public int GetNext() 
      {
          return m_RandomGenerator.Next(100);
      }
   }
  </code></pre>
  
  <p>The <code>Dictionary</code> instance is loaded each time the program starts, and this code will return the <strong>exact same sequence</strong> of numbers each time it is run. By which I mean, each time the executable is run. Surely the time value it's seeded with should be different (DateTime.Now.Ticks I assume?).</p>
  
  <p>A couple of points:</p>
  
  <ul>
  <li>There is only one instance of Dictionary, deserialized from a previously-exported file at startup. </li>
  <li>If I make m_RandomGenerator static then the problem is fixed.</li>
  </ul>
  
  <p>Does anyone know why? I did double check that I'm not creating a new instance of Dictionary each time, so that's not the issue.</p>
  
  <hr>
  
  <p>Well, colour me embarrassed. As it turns out the culprit was the [Serializable()] attribute.  The dictionary class I was using was loaded from a previously exported file, which was obviously loading the seed back into Random(). Changing the variable to static meant that the seed value was no longer loaded from the previously serialised instance - hiding the issue.</p>
  
  <p>Thanks to all the people offering constructive advice!</p>