title: C# Generating random int - mystery
body: <p>I have found few interesting things (for me at least) while generating random integers and I cannot explain it to myself so I thought I will post it here.</p>
  
  <p>My needs were simple: I was generating random integral (Int32) IDs and was aiming to minimize collisions. The generating time wasn´t an issue.</p>
  
  <p>I have tried these methods for generating random integers:</p>
  
  <p>1.)</p>
  
  <pre><code>   return rnd.Next();
  </code></pre>
  
  <p>where rnd is class field of type Random with Seed from Method #3.</p>
  
  <p>2.)</p>
  
  <pre><code>   return rnd.Next(Int32.MinValue, Int32.MaxValue);
  </code></pre>
  
  <p>where rnd is again class field of type Random with Seed from Method #3.</p>
  
  <p>3.)</p>
  
  <pre><code>   var buffer = new byte[sizeof(int)];
     using (var rng = new RNGCryptoServiceProvider())
     {
          rng.GetBytes(buffer);
     }            
     return BitConverter.ToInt32(buffer, 0);
  </code></pre>
  
  <p>Note: I also tried to have that RNGCryptoServiceProvider as class field initialized once on initialization of the containg class to ease work of GC, but I took same time to generate so I thought this would be "more random".</p>
  
  <p>4.)</p>
  
  <pre><code>   return new Random(Method3()).Next();
  </code></pre>
  
  <p>5.)</p>
  
  <pre><code>   return new Random(Method3()).Next(Int32.MinValue, Int32.MaxValue);
  </code></pre>
  
  <p>I know, that creating new Random(int seed) on every call is time consuming, but there is less collision, right?</p>
  
  <p>Well, now the mystery part. I was asuming that most collisions would have the method #1 and #2 where #1 would be slightly faster and more collisionless and least collisions would have the method #4 and #5 where #4 would be slightly faster and more collisionless and method #3 would be some sort of compromise.</p>
  
  <p>So I made a test to prove my asumptions. I generated 10x (to make it average) one million of random numbers using every mentioned method and took the average number of collision and average time it took to generate one million numbers. The results (below) are a bit suprise to me.</p>
  
  <p>The results:duration is hours:minutes:seconds:miliseconds format</p>
  
  <pre><code>Method1: AvgCollisions: 235, AvgDuration: 00:00:00.3561967
  Method2: AvgCollisions: 116, AvgDuration: 00:00:00.4042033
  Method3: AvgCollisions: 115, AvgDuration: 00:00:04.6037259
  Method4: AvgCollisions: 234, AvgDuration: 00:00:09.2195856
  Method5: AvgCollisions: 233, AvgDuration: 00:00:09.1788223
  </code></pre>
  
  <p>I ran the test few times again and it was always circa the same.</p>
  
  <p>Isn't it strange to you, too? The times aren't suprise at all that is what I assumed, but the results mean to me, that method2 is the best to generate random numbers, as it is the most random, the fastest and you can set a minimal and maximal generated number. Don't know how much is the Method2 more predictable over the Method3 as I don't know how would I test it. </p>
  
  <p>Can someone explain me what am I doning wrong or why methods #4 and #5 don't have the least collisions and why is the percentage of collisions always the same? Shouldn't it be random?</p>
  
  <p><strong>EDIT:</strong>
  Here is Visual Studio 2010 Solution of this test I've done: <a href="http://bit.ly/nxLODw" rel="nofollow">http://bit.ly/nxLODw</a></p>