Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>I am looking for a more in depth reason as to why the cooked up method may be more likely to generate collisions given the same degrees of freedom as a Guid.</p> </blockquote> <p>First, as others have noted, <code>Random</code> is not thread-safe; using it from multiple threads can cause it to corrupt its internal data structures so that it always produces the same sequence. </p> <p>Second, <code>Random</code> is seeded based on the current time. Two instances of <code>Random</code> created within the same millisecond (recall that a millisecond is several <em>million</em> processor cycles on modern hardware) will have the same seed, and therefore will produce the same sequence.</p> <p>Third, I lied. <code>Random</code> is not seeded based on the current time; it is seeded based on <em>the amount of time the machine has been active</em>. The seed is a 32 bit number, and since the granularity is in milliseconds, that's only a few weeks until it wraps around. But that's not the problem; the problem is: <strong>the time period in which you create that instance of <code>Random</code> is highly likely to be within a few minutes of the machine booting up.</strong> Every time you power-cycle a machine, or bring a new machine online in a cluster, there is a small window in which instances of Random are created, and the more that happens, the greater the odds are that you'll get a seed that you had before.</p> <p>As other have said: if you want a primary key for your database then <em>have the database generate you a primary key</em>; let the database do its job. If you want a globally unique identifier then <strong>use a guid</strong>; that's what they're for.</p> <p>And finally, if you are interested in learning more about the uses and abuses of guids then you might want to read my "guid guide" series; part one is here:</p> <p><a href="http://blogs.msdn.com/b/ericlippert/archive/2012/04/24/guid-guide-part-one.aspx">http://blogs.msdn.com/b/ericlippert/archive/2012/04/24/guid-guide-part-one.aspx</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload