Store values in an array and check if exisits via loop

Cosmos75

Registered User.
Local time
Today, 12:26
Joined
Apr 22, 2002
Messages
1,280
I have code that generates a random alphanumeric string.

Say I am generating a 4 character alphanumeric string.

The code loops and generates the first character, checks if it can create any more passwords with that character.
If it can, it stores this character, it continues the loop to generate the second alphanumeric character.
If it can’t, it starts all over again at the first character.

What I would like to do is to somehow store the character(s) that the code determines is unusable as opposed to doing running through the code again. How would I accomplish that? Store it in an array?

Right now the way I am accomplishing that is to have it look at the strinig in the table with the right length of characters. If it is generating the first character, then count the number of records that have the start with the same character (or characters) that is generated (matching upper or lower case as well) and then count the number of records it finds and see if it is less than the total possible alphanumeric strings that begin with the generated first character (or first two characters if we are on the second character of the stringand so on)

Say at the point the code determines that the character is unusable (i.e. no more unique alphanumeric strings can be generated with this first character) and it does get stored in an array (if applicable). Would it be faster to check the newly generated character using the same method I have been using, or check it against what is in the array;
- If found in array (must be case-sensitive), start back all over to generate first character.
- If not found in array than use old method to check it still usable.

I hope I have explained it clearly enough, if not feel free to let me know.

Edit: I should also mention that I have no experience using arrays. Wanted to first find out if it is appropriate to use an array.
 
Last edited:
Cosmos,

I see where you are headed with this, but I would vote for
the simplistic approach. I would generate the whole password
then use a DLookUp to see if it is in use. If it is, just generate
a new one.

Even a 4 character alphanumeric password has over a
million combinations, so you are not THAT likely to have a
collision.

With your current approach, you can use recordsets and
to look for:

Code:
sql = "Select Password " & _
         "From   YourTable " & _
         "Where PassWord like '" & YourCandidate & "*'"
Set rst = dbs.OpenRecordset(sql)
If Not rst.EOF and Not rst.BOF Then
   ' Exists already - regenerate
End If

The string (not array) YourCandidate is just built by concatenating
random characters to the end until the appropriate length is
reached.

p.s. Use Randomize prior to calling the RND function and you
"should" have no trouble.

Just a vote to keep it simple,
Wayne
 
Cosmos,

I totally agree.

Wayne
 
Thank you, the two Waynes'

I am trying to create a password generator. Here's what I am currently using

Code:
Dim pwFound As String
pwfound = vbNullString

Set rst = myDb.OpenRecordset("tblPassword", dbOpenDynaset)

Do While Not rst.EOF And pwFound <> pw

    rst.FindNext ("[PassWord] = " & Chr(39) & pw & Chr(39))
    
    If rst.NoMatch Then
'        MsgBox "No Match"
        GoTo StopRstNextLoop:
    Else
        pwFound = rst.Fields("PassWord")
'        MsgBox "Found a Match : " & pwFound
        If pwFound = pwFind Then
            pwExists = True
            GoTo StopRstNextLoop:
        Else
'            MsgBox "Case Match Not Found"
        End If
    End If
    
Loop

StopRstNextLoop:
It is true that a 4 alphanumeric string (password in this case) has (10+26+26)^4 = 14,776,336 possible unique passwords. I've run my code to generate all possible passwords that with two characters.

Shortest time: First password took 1 attempt and 0.08 seconds.

Longest time: 3841st password (of a possible 3844) took 8546 (more than number of possible passwords) attempts and 403.26 seconds.

I graphed the time it took to generate a password against the # of passwor dbeing generated (First Second, ... , Last). THe time it takes grows expontentially with the number of exisiting passwords. I assume that it would also increase witht he number of characters in the password. But by the same token, the more characters, the more possiblities there are and the less reason for concern unless I am doing something like tagging all the molecules in the universe!

If I use a default of 8 characters the possibilities are 218,340,105,584,896!! I guess I shouldn't worry since there aren't even that number of people on this earth. But was wondering if it would be worth it to build that in an attempt to speed up password generation which would probably only show up once a big percentage (>90%) of possible passwords have been generated.

I did not even consider the memory overhead for using an array.

From another site;
If you use only the digits from 1 to 0, capital A to capital Z, and lower case A to lower Case z, each character position has 62 possible values ....resulting in 14,776,336 possible combinations. This would be a rather(!) large array requiring about 120 megabytes of memory if stored in a one-byte code array in memory or 120 mb if stored in two-byte unicode.
That in and of itself is a good reason not to use an array.

I guess this is one case were perfection is not required.
:p
 

Users who are viewing this thread

Back
Top Bottom