Note that there are some explanatory texts on larger screens.

plurals
  1. POGuid.NewGuid() VS a random string generator from Random.Next()
    primarykey
    data
    text
    <p>My colleague and I are debating which of these methods to use for auto generating user ID's and post ID's for identification in the database:</p> <p>One option uses a single instance of Random, and takes some useful parameters so it can be reused for all sorts of string-gen cases (i.e. from 4 digit numeric pins to 20 digit alphanumeric ids). Here's the code:</p> <pre><code>// This is created once for the lifetime of the server instance class RandomStringGenerator { public const string ALPHANUMERIC_CAPS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; public const string ALPHA_CAPS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; public const string NUMERIC = "1234567890"; Random rand = new Random(); public string GetRandomString(int length, params char[] chars) { string s = ""; for (int i = 0; i &lt; length; i++) s += chars[rand.Next() % chars.Length]; return s; } } </code></pre> <p>and the other option is simply to use:</p> <pre><code>Guid.NewGuid(); </code></pre> <p>see <a href="http://msdn.microsoft.com/en-us/library/system.guid.newguid.aspx">Guid.NewGuid on MSDN</a></p> <p>We're both aware that <code>Guid.NewGuid()</code> would work for our needs, but I would rather use the custom method. It does the same thing but with more control.</p> <p>My colleague thinks that because the custom method has been cooked up ourselves, it's more likely to generate collisions. I'll admit I'm not fully aware of the implementation of Random, but I presume it is just as random as Guid.NewGuid(). A typical usage of the custom method might be:</p> <pre><code>RandomStringGenerator stringGen = new RandomStringGenerator(); string id = stringGen.GetRandomString(20, RandomStringGenerator.ALPHANUMERIC_CAPS.ToCharArray()); </code></pre> <p><strong>Edit 1:</strong></p> <ul> <li>We are using Azure Tables which doesn't have an auto increment (or similar) feature for generating keys.</li> <li>Some answers here just tell me to use NewGuid() "because that's what it's made for". I'm 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.</li> </ul> <p><strong>Edit 2:</strong></p> <p>We were also using the cooked up method to generate post ID's which, unlike session tokens, need to look pretty for display in the url of our website (like <a href="http://mywebsite.com/14983336">http://mywebsite.com/14983336</a>), so guids are not an option here, however collisions are still to be avoided.</p>
    singulars
    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.
 

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