title: How Random is System.Guid.NewGuid()? (Take two)
body: <p>Before you start marking this as <a href="https://stackoverflow.com/questions/467271/how-random-is-system-guid-newguid">a duplicate</a>, read me out. The other question has a (most likely) incorrect accepted answer.</p>
  
  <p>I do not know how .NET generates its GUIDs, probably only Microsoft does, but there's a high chance it simply calls <a href="http://msdn.microsoft.com/en-us/library/ms688568(VS.85).aspx" rel="noreferrer">CoCreateGuid()</a>. That function however is documented to be calling <a href="http://msdn.microsoft.com/en-us/library/aa379205(VS.85).aspx" rel="noreferrer">UuidCreate()</a>. And the algorithms for creating an UUID are <a href="http://en.wikipedia.org/wiki/UUID" rel="noreferrer">pretty well documented</a>.</p>
  
  <p>Long story short, be as it may, it seems that <code>System.Guid.NewGuid()</code> indeed uses <a href="http://en.wikipedia.org/wiki/UUID#Version_4_.28random.29" rel="noreferrer">version 4 UUID generation algorithm</a>, because all the GUIDs it generates matches the criteria (see for yourself, I tried a couple million GUIDs, they all matched).</p>
  
  <p>In other words, these GUIDs are <em><strong>almost</strong></em> random, except for a few known bits.</p>
  
  <p>This then again raises the question - how random <strong>IS</strong> this random? As every good little programmer knows, a pseudo-random number algorithm is only as random as its seed (aka entropy). So what is the seed for <code>UuidCreate()</code>? How ofter is the PRNG re-seeded? Is it cryptographically strong, or can I expect the same GUIDs to start pouring out if two computers accidentally call <code>System.Guid.NewGuid()</code> at the same time? And can the state of the PRNG be guessed if sufficiently many sequentially generated GUIDs are gathered?</p>
  
  <p><strong>Added:</strong> To clarify, I'd like to find out how random can I trust it to be and thus - where can I use it. So, let's establish a rough "randomness" scale here:</p>
  
  <ol>
  <li>Basic randomness, taking current time as the seed. Usable for shuffling cards in Solitaire but little else as collisions are too easy to come by even without trying.</li>
  <li>More advanced randomness, using not only the time but other machine-specific factors for seed. Perhaps also seeded only once on system startup. This can be used for generating IDs in a DB because duplicates are unlikely. Still, it's not good for security because the results can be predicted with sufficient effort.</li>
  <li>Cryptograhpically random, using device noise or other advanced sources of randomness for seed. Re-seeded on every invocation or at least pretty often. Can be used for session IDs, handed out to untrusted parties, etc.</li>
  </ol>
  
  <p>I arrived at this question while thinking if it would be OK to use them as DB IDs, and whether the <a href="http://www.informit.com/articles/article.aspx?p=25862" rel="noreferrer">Guid.comb</a> algorithm implementation together with <code>System.Guid.NewGuid()</code> (like NHibernate does it) would be flawed or not.</p>