title: Obscure VBMath random numbers generator behavior
body: <p>I want to repeat a random number sequence generated by a legacy software using the VBMath.Rnd and VBMath.Randomize functions in VB .NET</p>
  
  <p>Reading on the documentation for those functions on <a href="http://msdn.microsoft.com/en-us/library/ht1sfkd6.aspx" rel="nofollow noreferrer">MSDN</a> i found that you are supposed to "reset" the generator calling Rnd with a negative value if you want the same seed to give you the same result sequence each time.</p>
  
  <p>But doing some tests... things didn't work as expected.</p>
  
  <p>The legacy software does something like this at the start of the application on different executions:</p>
  
  <pre><code>float[] rNums = new float[4];
  
  VBMath.Randomize(154341.77394338892);
  for (int index = 0; index &lt; 4; index++)
  {
      rNums[index] = VBMath.Rnd();
  }
  </code></pre>
  
  <p>And my code does something like this:</p>
  
  <pre><code>VBMath.Rnd(-1);
  VBMath.Randomize(154341.77394338892);
  for (int index = 0; index &lt; 4; index++)
  {
      Console.WriteLine("rNum[" + index + "] " + rNums[index] + " = " + VBMath.Rnd());
  }
  </code></pre>
  
  <p>The results for this test are:</p>
  
  <pre><code>rNum[0] 0,6918146 = 0,2605162
  rNum[1] 0,5121228 = 0,4748411
  rNum[2] 0,8309224 = 0,8112976
  rNum[3] 0,972851  = 0,8011347
  </code></pre>
  
  <p>The sequence that i want to reproduce in the second code any number of times is the sequence generated from the hard coded initial  state of the generator. That means the sequence you would get if you run the first code alone.</p>
  
  <p>I can not change the first code.</p>
  
  <p>Any idea on why the VBMath.Rnd and VBMath.Randomize functions arent working as expected?</p>
  
  <p>Did i miss something?</p>
  
  <hr>
  
  <h2>ANSWER</h2>
  
  <p>Thee problem is that since the legacy code doesn't call Rnd with a negative value, the generator doesn't clear its state and the call to Rnd gets chained to the previous value of the seed (in this case, the hard-coded value). </p>
  
  <p>To solve the problem and be able to repeat the process all over again without all the problems that would imply "reproducing" the initial state, i cloned the generator code and patched it so i could reproduce the same situation every time depending on a parameter.</p>
  
  <p>I know.. its ugly.. but it solves my problem (Btw i also know that there are some rounding errors and that the generated values are not exact.. they differ in like the last digit or something) but i don't need exact precision. </p>
  
  <p>The rounding error probably comes from my choice of language for the cloning of the algorithm. If someone could help out on how to get the exact same result (match the rounding errors) that would be nice.</p>
  
  <p>The patched code follows.</p>
  
  <pre><code>public sealed class RndGenerator
  {
      static int m_rndSeed = 0x50000;
      // This is the value that the programmer sets the seed at ProjectData object
      // initialization
      const int CONSTANT_INIT_RNDSEED = 0x50000; 
  
      // Methods
      private static float GetTimer()
      {
          DateTime now = DateTime.Now;
          return (float)(((((60 * now.Hour) + now.Minute) * 60) + now.Second) + (((double)now.Millisecond) / 1000.0));
      }
  
      public static void Randomize()
      {
          float timer = GetTimer();
          int rndSeed = m_rndSeed;
          int num = BitConverter.ToInt32(BitConverter.GetBytes(timer), 0);
          num = ((num &amp; 0xffff) ^ (num &gt;&gt; 0x10)) &lt;&lt; 8;
          rndSeed = (rndSeed &amp; -16776961) | num;
          m_rndSeed = rndSeed;
      }
  
      public static void Randomize(double Number)
      {
          Randomize(Number, false);
      }
  
      public static void Randomize(double Number, bool useHardCodedState)
      {
          int num;
  
          int rndSeed = 0;
          if (useHardCodedState)
              rndSeed = CONSTANT_INIT_RNDSEED;
          else
              rndSeed = m_rndSeed;
  
          if (BitConverter.IsLittleEndian)
          {
              num = BitConverter.ToInt32(BitConverter.GetBytes(Number), 4);
          }
          else
          {
              num = BitConverter.ToInt32(BitConverter.GetBytes(Number), 0);
          }
          num = ((num &amp; 0xffff) ^ (num &gt;&gt; 0x10)) &lt;&lt; 8;
          rndSeed = (rndSeed &amp; -16776961) | num;
          m_rndSeed = rndSeed;
      }
  
      public static float Rnd()
      {
          return Rnd(1f);
      }
  
      public static float Rnd(float Number)
      {
          int rndSeed = m_rndSeed;
          if (Number != 0.0)
          {
              if (Number &lt; 0.0)
              {
                  long num3 = BitConverter.ToInt32(BitConverter.GetBytes(Number), 0);
                  num3 &amp;= (long)0xffffffffL;
                  rndSeed = (int)((num3 + (num3 &gt;&gt; 0x18)) &amp; 0xffffffL);
              }
              rndSeed = (int)(((rndSeed * 0x43fd43fdL) + 0xc39ec3L) &amp; 0xffffffL);
          }
          m_rndSeed = rndSeed;
          return (((float)rndSeed) / 1.677722E+07f);
      }
  }
  </code></pre>