When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sections:
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
XML Info
Information:
Feedback
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
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): 230382
220112
187654

Generated with generatePassword(False): WJuoAt
OLYIGm
XbBPND

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!

Return to user tips...


ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article