title: Most Efficient Way to... Unique Random String
body: <p>I need to efficently insert a 5 character RANDOM string into a database while also ensuring that it is UNIQUE.  Generating the random string is not the problem, but currently what I am doing is generating the string and then checking the DB if it exists already... if it does, I start over.</p>
  
  <p>Is there a more efficient way to do this process?</p>
  
  <p>Please note, I do NOT want to use GUID or anything else that is more than 5 Characters.... I MUST stick to 5 Characters.</p>
  
  <p>PS: I don't think it makes a difference, but my strings are all case sensitive.</p>
  
  <p>Here is the "Random String" portion</p>
  
  <pre><code>    Public Function GetRandomNumbers(ByVal numChars As Integer) As String
      Dim chars As String() = { _
       "A", "B", "C", "D", "E", "F", _
       "G", "H", "I", "J", "K", "L", _
       "M", "N", "O", "P", "Q", "R", _
       "S", "T", "U", "V", "W", "X", _
       "Y", "Z", "0", "1", "2", "3", _
       "4", "5", "6", "7", "8", "9", _
       "a", "b", "c", "d", "e", "f", _
       "g", "h", "i", "j", "k", "l", _
       "m", "n", "o", "p", "q", "r", _
       "s", "t", "u", "v", "w", "x", _
       "y", "z"}
      Dim rnd As New Random()
      Dim random As String = String.Empty
      Dim i As Integer = 0
      While i &lt; numChars
          random += chars(rnd.[Next](0, 62))
          System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
      End While
      Return random
  End Function
  </code></pre>