I have to efficently place a 5 character RANDOM string right into a database whilst making certain that it's UNIQUE. Producing the random string isn't the problem, but presently things i am doing is producing the string after which checking the DB whether it is available already... whether it does, I begin again.

It is possible to more effective method of doing this method?

Please be aware, I don't want to make use of GUID or other things that's a lot more than 5 Figures.... I Have To stay with 5 Figures.

PS: I do not think it is important, but my strings are situation sensitive.

This is actually the "Random String" portion

    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 < numChars
        random += chars(rnd.[Next](0, 62))
        System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
    End While
    Return random
End Function

Produce a table having a large pool of 5-character strings which are put in sequence (so that they are unique), and also have a GUID his or her primary key. Give a column to point whether they are utilised or otherwise.

When you really need a brand new number, you choose top 1 in the pool, order through the guid (therefore it becomes random), and hang the end result as "spent".

You can produce a GUID and just make use of the first 5 figures?

Is randomness more essential, or perhaps is originality more essential? -- observe that I stated "more" important I recieve because you need both.

If randomness is much more important, then you are have to a way to trace historic values. The database itself (by having an appropriate index) will probably be the easiest method to do that.

If originality is much more important, then simply just make use of a counter and zero-pad it to 5 numbers. This can, obviously, limit you to definitely 100,000 rows, which means you could alternatively make use of a counter along with a transformation into character space (eg, 1 = "A", 2 = "B", 27 = "AA", and so forth).

There's a means to pick unused unique words by random, but it is most likely not really much better than your work now.

The key is you pick which permutations from the words which are unused, produce a random number according to the number of unused permuations you will find, and pick that certain.

Should you for instance would make use of a word with three figures, and just the smoothness and 1, you will find eight possible permutations. Should you already used the combinations "010" and "100", you can get something which appears like this:

PI = permutation index
UI = unused permutation index

No. PI UI
----------
000 0  0
001 1  1
010 2  -
011 3  2
100 4  -
101 5  3
110 6  4
111 7  5

To choose an unused permutation, you just produce a random number from to five, and select the corresponding permutation.

Keeping a listing of possible permuations is absolutely not practical, which means you would want a function that may determine the permutation index in the string, and something function that may determine the string in the permutation index.

Also, to find out which permutations are unused, you need to check that are used, so you've still got to question the table sooner or later.

If you're placing the string for an existing, populated, table then you'll always need to see if the string does not exist there (it does not need to be an explicit Choose). You may either into it by hand, or come with an UNIQUE constraint around the column and allow the database to get it done. Therefore if the database returns a mistake since the string has already been there, generate a different one.

Observe that for those who have a clear table and wish to populate it with multiple random strings, it is a different problem.