GetRandom

Generate random string, fully customization, with even deciding what special characters.
Update: New version forcing first letter to be either upper or lower case letter if either is allowed, to prevent having random starting with number.


Public

Tested

My Own Work
Function GetRandom(intMinLen, intMaxLen, UseNumbers, UseUpperCase, UseLowerCase, UseSpcChars,strSpecialchars)
 Dim intLen, strRnd
 If IntMinLen = "" then intMinLen=25
 If IntMinLen = 0 then intMinLen=25
 If IntMaxLen = "" then intMaxLen=25
 If IntMaxLen = 0 then intMaxLen=25
 If UseNumbers = "" then UseNumbers=1
 If UseUpperCase = "" then UseUpperCase=1
 If UseLowerCase = "" then UseLowerCase=1
 If UseSpcChars = "" then UseSpcChars=1
 If strSpecialChars="" then strSpecialChars="#$^*()_-" ' "!@#$%^&*()_+-/\|"

 ' Generates a random number with length: iMinLen to iMaxLen (example 20 to 30 long)
 Randomize
strRnd     = ""
 intLen     = intMaxLen
 If intMinLen <> intMaxLen then _
  intLen = Int((intMaxLen - intMinLen + 1) * Rnd + intMinLen)
 ' now depending on the length of the password (dependent on the random number generated above), create random characters between a-z, A-Z, 0-9 or one char from the strSpecialChars above by using a for loop
 Do While Len(strRnd) < intLen
  ' Generates a random number: 1, 2, 3, or 4; where
  ' 1 gets a lowercase letter; 2 gets uppercase character, and
  ' 3 gets a number between 0 and 9
  ' 4 gets special characters from strSpecialChars
  Randomize
  intCharType   = Int(4 * Rnd + 1) ' now check if intCharType is 1, 2, 3, or 4
  If strRnd = "" Then ' forcing first letter to be uppercase or lowercase, no number nor special character
   If UseLowerCase = 1 Then intCharType = 1
   If UseUpperCase = 1 Then intCharType = 2
  End If
  Select Case intCharType
  Case 1 ' get a lowercase letter a-z inclusive
   If UseLowerCase = 1 Then
    Randomize
    strRnd  = strRnd & Chr(Int((25 * Rnd) + 97))
   End If
  Case 2 ' get a uppercase letter A-Z inclusive
   If UseUpperCase = 1 Then
    Randomize
    strRnd  = strRnd & Chr(Int((25 * Rnd) + 65))
   End If
  Case 3 ' get a number between 0 and 9 inclusive
   If UseNumbers = 1 Then
    Randomize
    strRnd  = strRnd & Chr(Int((9 * Rnd) + 48))
   End If
  Case 4
   ' get a number between 0 and len(strSpecialChars)
   If UseSpcChars = 1 Then
    Randomize
    strRnd  = strRnd & Mid(strSpecialChars, Int((Len(strSpecialChars) * Rnd) + 1), 1)
   End If
  End Select
 Loop
 GetRandom    = strRnd ' return password
End Function

intMinLen, intMaxLen, UseNumbers, UseUpperCase, UseLowerCase, UseSpcChars,strSpecialchars

Views 3886 Downloads 1168

Classic ASP Texts + Strings
ANmarAmdeen
719
Attachments
Revisions

v2.0