User Tips: Randomly Generating an Alpha or Numeric Random Password
By Vincent A.
Have you ever needed to generate a random password for your visitors? The following function,
generatePassword
, will return a randomly generated password consisting either of all numeric or
all alpha characters. generatePassword
expects one parameter passed in, a boolean. If this
is True
, the returned random password will be all numeric; else it will be all alpha.
Here is some sample output of the generatePassword
function:
Generated with generatePassword(True) :
130130 130740 087481
Generated with generatePassword(False) :
XDMTbL PFQNhE YvSUOv
|
Here is the source code for the function generatePassword
. Feel free to cut and paste this into
your applications!
Function generatePassword( allowNumbers )
NUMLOWER = 48 ' 48 = 0
NUMUPPER = 57 ' 57 = 9
LOWERBOUND = 65 ' 65 = A
UPPERBOUND = 90 ' 90 = Z
LOWERBOUND1 = 97 ' 97 = a
UPPERBOUND1 = 122 ' 122 = z
PASSWORD_LENGTH = 6
' initialize the random number generator
Randomize()
newPassword = ""
count = 0
DO UNTIL count = PASSWORD_LENGTH
If allowNumbers Then
pwd = Int( ( NUMUPPER - NUMLOWER ) * Rnd + NUMLOWER )
Else
' generate a num between 2 and 10 ;
' if num > 4 create an uppercase else create lowercase
If Int( ( 10 - 2 + 1 ) * Rnd + 2 ) > 4 Then
pwd = Int( ( UPPERBOUND - LOWERBOUND + 1 ) * Rnd + LOWERBOUND )
Else
pwd = Int( ( UPPERBOUND1 - LOWERBOUND1 + 1 ) * Rnd + LOWERBOUND1 )
End If
End If
newPassword = newPassword + Chr( pwd )
count = count + 1
Loop
generatePassword = newPassword
End Function
|
I hope this function can be of use to you! If you are interested in generating random passwords be sure to
read these other articles...
Happy Programming!