|
|
|
Each page contains a helpful programming tip and exercises which encourage
beginners to use what they've learned in a different situation.
|
Creating Random Passwords
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 email address. Then you create a random password, assign it to that email address, and fire off an email
to 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 bogus
email 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? 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.
Other Scenarios
Let's write functions to create other kinds of random passwords.
- A six digit password containing all numbers
- An eight digit password which has three numbers at the end
- A six digit password which alternates between upper and lower case
- A seven digit password which alternates between vowels and consonants
"Beginner Programming Tips and Tricks" is written by Douglas Twitchell, and hosted at The Problem Site.
Contents copyright 2005 by Douglas Twitchell. Contents of this page may not be reproduced without permission of the author. For information on using
this site in a classroom situation, please visit the Teachers page.
More programming information and other tips can be found at Virtu Software's Ask Doug site.
| |
|
Search For More Educational Resources
|
|
|