Games
Problems
Go Pro!

Generating Random Passwords

Reference > Science > Technology > Beginner Programming Tips
 

Creating 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:
 

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 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 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, 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:
 

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?). Once you 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


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.

Questions

1.
Write code to create a six-digit password containing all numbers.
2.
Write code to create an eight-digit password which has three numbers at the end.
3.
Write code to create a six-digit password which alternates between upper and lower case.
4.
Write code to create a seven-digit password which alternates between vowels and consonants.
Assign this reference page
Click here to assign this reference page to your students.
Randomizing an Array of IndicesRandomizing an Array of Indices
Validating E-mail Address FormatValidating E-mail Address Format
 

Ask Professor Puzzler

Do you have a question you would like to ask Professor Puzzler? Click here to ask your question!
Over 3,000 Pages of Free Content
We've been providing free educational games and resources since 2002.

Would you consider a donation of any size to help us continue providing great content for students of all ages?

Like us on Facebook to get updates about new resources
Home
Pro Membership
About
Privacy