When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles [1.x] [2.0]
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
Headlines from ASPWire.com
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML Info
Information:
Advertise
Feedback
Author an Article
Technology Jobs



















internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.

Finance Developer (IL)
Next Step Systems
US-IL-Chicago

Justtechjobs.com Post A Job | Post A Resume

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): 050723
040434
817175

Generated with generatePassword(False): CSXdEI
TVBxKA
dkEERS

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...


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


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers