title: Creating your own Tinyurl style uid
body: <p>I'm writing a small article on humanly readable alternatives to Guids/UIDs, for example those used on TinyURL for the url hashes (which are often printed in magazines, so need to be short).</p>
  
  <p>The simple uid I'm generating is - 6 characters: either a lowercase letter (a-z) or 0-9. </p>
  
  <p>"According to my calculations captain", that's 6 mutually exclusive events, although calculating the probability of a clash gets a little harder than P(A or B) = P(A) + P(B), as obviously it includes numbers and from the code below, you can see it works out whether to use a number or letter using 50/50.</p>
  
  <p>I'm interested in the clash rate and if the code below is a realistic simulation of anticipated clash rate you'd get from generating a hash. On average I get 40-50 clashes per million, however bare in mind the uid wouldn't be generated a million times at once, but probably only around 10-1000 times a minute.</p>
  
  <p>What is the probability of a clash each time, and can anyone suggest a better way of doing it?</p>
  
  <pre><code>static Random _random = new Random();
  
  public static void main()
  {
      // Size of the key, 6
      HashSet&lt;string&gt; set = new HashSet&lt;string&gt;();
      int clashes = 0;
      for (int n=0;n &lt; 1000000;n++)
      {
          StringBuilder builder = new StringBuilder();
  
          for (int i =0;i &lt; 7;i++)
          {
              if (_random.NextDouble() &gt; 0.5)
              {
                  builder.Append((char)_random.Next(97,123));
              }
              else
              {
                  builder.Append(_random.Next(0,9).ToString());
              }
          }
  
          if (set.Contains(builder.ToString()))
          {
              clashes++;
              Console.WriteLine("clash: (" +n+ ")" +builder.ToString());
          }
  
          set.Add(builder.ToString());
          _random.Next();
          //Console.Write(builder.ToString());
      }
  
      Console.WriteLine("Clashes: " +clashes);
      Console.ReadLine();
  }
  </code></pre>
  
  <p><strong>UPDATE:</strong> 
  <a href="http://www.yetanotherchris.me/home/2009/3/3/friendly-unique-id-generation-part-1.html" rel="nofollow noreferrer">Here's the resulting article</a> from this question</p>
  
  <p>I really asked two questions here so I was cheating. The answer I was after was rcar's, however Sklivvz's is also the answer to the 2nd part (an alternative). Is it possible to make a custom unique id generator in a database, or would it be client side (which would be 2 possible reads first)?</p>
  
  <p>The general idea I was after was using Ids in databases or other stores that can be used by phone or printed material, not a giant 16 byte guid.</p>
  
  <p><strong>UPDATE 2:</strong> I put the formula for two mutually exclusive events above instead of 2 independent ones (as getting an 'a' the first time doesn't mean you can't get an 'a' the second time). Should've been P(A and B) = P(A) x P(B) </p>