Generating Random Passwords
Reference > Science > Technology > Beginner Programming TipsCreating a random password is a programming task you may have to handle if you own a website which has membership options. A visitor signs up by entering their e-mail address. Then you create a random password, assign it to that e-mail address, and fire off an e-mail to the visitor with the random password in the e-mail.
Why would you do this? It's a way of verifying that the visitor has entered a valid e-mail address. If they entered a bogus e-mail address, the e-mail 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? Of course, you'll need your old friend the "RND" function, which we've talked about before. You'll also need to decide what you want your password to look like. Do you want it to be just letters? Letters and numbers? Just numbers? Combinations of vowels and consonants?
Let's start with the simplest one; we're going to create a random password containing nothing but upper case letters. To do this, we need to find out what the ASCII values for upper case letters are. The letter A is character code 65, and the letter Z is 90. If you're not sure how to generate a random number between two values, you'll want to review this page: Generating Random Numbers.
Here's what the code will look like:
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 long do you want your password to be? Let's say it'll be 6 characters. So we need to do a loop six times:
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, so websites will often create random passwords which alternate between consonants and vowels. By doing this, you make it possible for the visitor to pronounce the password, which makes it easier to remember. (I do this little trick on The Treasure Hunt.)
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:
'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?). Once you have both of these functions, creating your random password is simple as:
Dim Index As Integer
Randomize Timer
'Get Random Letters
For Index = 1 to 3
ThePassword = ThePassword & GetRandomConsonant()
ThePassword = ThePassword & GetRandomVowel()
Next Index
What is the other way of handling this? The other way would be to create a single function titled IsVowel(), which takes a string as its input, and returns true or false depending on whether the string is a vowel or not. Now you can pick any letter, check to see if it's a vowel or not, and then decide whether to use it or not. The advantage to this approach is, it saves you the trouble of creating the much more lengthy GetRandomConsonant function. The disadvantage is that you can end up using more processing time, because you randomly pick letters and then don't use them.