vendredi 1 novembre 2019

Why does my "random key code" creates a same key every once in a while?

I`m having the following code from which I extract randomPW for my db.

I need this string of random characters in order to use it a primary key in my Db. The problem is that I`m getting quite a lot of duplicates when I execute this code more than once or if I get a Loop in order to extract (for example) 100 keys at once.

If I try to reload the page in order to insert one by one key the same problem occurs... every 50-80 reloads there is a duplicate. Can you please tell me what`s wrong with my code?

    <%
    Function RandomPW(myLength)
    Const minLength = 6
    Const maxLength = 20

    Dim X, Y, strPW

    If myLength = 0 Then
    Randomize
    myLength = Int((maxLength * Rnd) + minLength)
    End If


    For X = 1 To myLength
    Y = Int((3 * Rnd) + 1) '(1) Numeric, (2) Uppercase, (3)   Lowercase

    Select Case Y
    Case 1
    'Numeric character
    Randomize
    strPW = strPW & CHR(Int((9 * Rnd) + 48))
    Case 2
    'Uppercase character
    Randomize
    strPW = strPW & CHR(Int((25 * Rnd) + 65))
    Case 3
    'Lowercase character
    Randomize
    strPW = strPW & CHR(Int((25 * Rnd) + 97))

    End Select
    Next    
    RandomPW = strPW
    End Function
    %>

I expect my code to extract a sting that will not duplicate every now and then.




Aucun commentaire:

Enregistrer un commentaire