Function generatePassword( passwordLength )
Dim sDefaultChars
Dim iCounter
Dim sMyPassword
Dim iPickedChar
Dim iDefaultCharactersLength
Dim iPasswordLength
sDefaultChars="abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789"
iPasswordLength=passwordLength
iDefaultCharactersLength = Len(sDefaultChars)
Randomize 'initialize the random number generator
For iCounter = 1 To iPasswordLength
'Next pick a number from 1 to length of character set
iPickedChar = Int((iDefaultCharactersLength * Rnd) + 1)
'Next pick a character from the character set using the random number iPickedChar and Mid function
sMyPassword = sMyPassword & Mid(sDefaultChars,iPickedChar,1)
Next
generatePassword = sMyPassword
End Function
Function generatePassword(passwordLength)
Dim sDefaultChars
Dim iCounter
Dim sMyPassword
Dim iPickedChar
Dim iDefaultCharactersLength
Dim iPasswordLength
Dim UC_Count, LC_Count, Num_Count, Special_Count
Dim TempPWD
Dim boolAddit
Static oRE
Dim oMatches
If passwordLength < 7 Then '7 is min pwd length
Error 5000
Exit Function
End If
If IsEmpty(oRE) Then
ReDim oRE(3)
Set oRE(0) = CreateObject("vbscript.regexp")
oRE(0).Global = True
oRE(0).Pattern = "[A-Z]"
Set oRE(1) = CreateObject("vbscript.regexp")
oRE(1).Global = True
oRE(1).Pattern = "[a-z]"
Set oRE(2) = CreateObject("vbscript.regexp")
oRE(2).Global = True
oRE(2).Pattern = "[0-9]"
Set oRE(3) = CreateObject("vbscript.regexp")
oRE(3).Global = True
oRE(3).Pattern = "[~!@#$%^+-]"
End If
sDefaultChars = "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789~!@#$%^+-"
iPasswordLength = passwordLength
iDefaultCharactersLength = Len(sDefaultChars)
Randomize 'initialize the random number generator
Do
'Next pick a character from the candidate list
iPickedChar = Int((iDefaultCharactersLength * Rnd) + 1)
TempPWD = sMyPassword & Mid(sDefaultChars, iPickedChar, 1)
Set oMatches = oRE(0).Execute(TempPWD)
UC_Count = oMatches.Count
Set oMatches = oRE(1).Execute(TempPWD)
LC_Count = oMatches.Count
Set oMatches = oRE(2).Execute(TempPWD)
Num_Count = oMatches.Count
Set oMatches = oRE(3).Execute(TempPWD)
Special_Count = oMatches.Count
boolAddit = ((UC_Count <= 1) And (LC_Count <= 3) And (Num_Count <= 2) And (Special_Count <= 1))
If Len(sMyPassword) < 7 Then
If boolAddit Then
sMyPassword = TempPWD
End If
Else
sMyPassword = TempPWD
End If
Loop Until Len(sMyPassword) = iPasswordLength
generatePassword = sMyPassword
End Function
Note: There are some weaknesses in the VB PRNG that I outlined in this article.
From you initial set, it seems that only upper and lowercase characters are possible. Add numbers and special characters as well to the string.