Creating a random password is a programming task you may have to handle if you own a website which has membership options. A visitorsigns up by entering their email address. Then you create a random password, assign it to that email address, and fire off an emailto the visitor with the random password in the email.

Why would you do this? It's a way of verifying that the visitor has entered a valid email address. If they entered a bogusemail address, the email will never get to them, and they'll never find out what their password is, and they won't be able to log in.

So, how do you create a randomly generated password

Here's what the code will look like:


Dim RandomLetter As Integer
Randomize Timer
'Get One Random Letter
RandomLetter = Int(Rnd * 26) + 65

Of course, that's just one numeric value, and we need a bunch of them strung together as a string variable. How longdo you want your password to be? Let's say it'll be 6 characters. So we need to do a loop six times:

Dim RandomLetter As Integer
Dim ThePassword As String
Dim Index As Integer
Randomize Timer
'Get Random Letters
For Index = 1 to 6
RandomLetter = Int(Rnd * 26) + 65
ThePassword = ThePassword & Chr(RandomLetter)
Next Index

That'll do it! You now have a random password containing nothing but upper case letters.

One thing that most people will tell you, though, is that passwords like this are very difficult to remember, sowebsites will often create random passwords which alternate between consonants and vowels. By doing this, you make itpossible for the visitor to pronounce the password, which makes it easier to remember.

So, how do we go about doing this? Well, there are a couple ways you can do it. One way is to create a function that generates vowels:


Randomize Timer

'Get Random Vowel
Function GetRandomVowel() As String
Dim RandomVowel As Integer
RandomVowel = Int(Rnd * 5)
Select Case RandomVowel
Case 0:
GetRandomVowel = "A"
Case 1:
GetRandomVowel = "E"
Case 2:
GetRandomVowel = "I"
Case 3:
GetRandomVowel = "O"
Case 4:
GetRandomVowel = "U"
End Select
End Function

Then, of course, you create another function which generates a random consonant (this function will be a lot longer, right?). Onceyou have both of these functions, creating your random password is simple as:

Dim ThePassword As String
Dim Index As Integer
Randomize Timer
'Get Random Letters
For Index = 1 to 3
ThePassword = ThePassword & GetRandomConsonant()
ThePassword = ThePassword & GetRandomVowel()
Next Index