Generate random password and insert value in variable

I am using classic ASP/VB
I need to generate a random password, which should include two numbers, two characters (@#$%, etc) and two letters in caps.
And then I need to create a variable and make this variable be equal to the random password.

I have an old script that generates a password but:

1. I am not sure if it meets the requirements above
2. I am not sure how to make a variable hold the password value
Also has to be 8 characters long

Sub StrRandomize(strSeed)
        Dim i, nSeed
        nSeed = CLng(0)
        For i = 1 To Len(strSeed)
                nSeed = nSeed Xor ((256 * ((i - 1) Mod 4) * AscB(Mid(strSeed, i, 1))))
        Next
        'Randomiser
        Randomize nSeed
End Sub
'--GeneratePassword(nLength)
'--Generates friendly passwords for remembering and pronounciation
Function GeneratePassword(nLength)
        Dim i, bMadeConsonant, c, nRnd
		'You may adjust the below constants to include local,
		'eg. scandinavian characters. This way your passwords
		'will not be limited to latin characters.
        Const strDoubleConsonants = "bdfglmnpst"
        Const strConsonants = "bcdfghklmnpqrstv"
        Const strVocal = "aeiou"
        GeneratePassword = ""
        bMadeConsonant = False
        For i = 0 To nLength
                'Get a random number number between 0 and 1
                nRnd = Rnd
                'Simple or double consonant, or a new vocal?
                'Does not start with a double consonant
				'15% or less chance for the next letter being a double consonant
                If GeneratePassword <> "" AND (bMadeConsonant <> True) AND (nRnd < 0.15) Then
                        'double consonant
                        c = Mid(strDoubleConsonants, Int(Len(strDoubleConsonants) * Rnd + 1), 1)
						'response.write int(Len(strDoubleConsonants) * Rnd + 1)
						'response.write "<br>"
                        c = c & c
						i = i + 1
                        bMadeConsonant = True
                Else
						'80% or less chance for the next letter being a consonant,
						'depending on wether the last letter was a consonant or not.
                        If (bMadeConsonant <> True) And (nRnd < 0.95) Then
                                'Simple consonant
                                c = Mid(strConsonants, Int(Len(strConsonants) * Rnd + 1), 1)
                                bMadeConsonant = True
						'5% or more chance for the next letter being a vocal. 100% if last
						'letter was a consonant - theoreticaly speaking...
                        Else
                                'If last one was a consonant, make vocal
                                c = Mid(strVocal,Int(Len(strVocal) * Rnd + 1), 1)
                                bMadeConsonant = False
                        End If
                End If
                'Add letter
                GeneratePassword = GeneratePassword & c
        Next
        'Is the password long enough, or perhaps too long?
        If Len(GeneratePassword) > nLength Then
                GeneratePassword = Left(GeneratePassword, nLength)
        End If
End Function

StrRandomize CStr(Now) & CStr(Rnd)

Open in new window

LVL 1
AleksAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
This is VB6 code so it may need modification. Note that it uses 31 symbols and if you don't want to use them all, delete those you don't want from the SYMBOLS constant and adjust the references to 31 accordingly.
    Const LETTERS As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    Const SYMBOLS As String = "~!@#$%^&*()_+-={}|[]\:"";'<>?,./"
    Dim strPassword As String
    Dim intIndex As Integer
    
    Randomize
    
    ' Generate the password string making the 1st character a letter
    strPassword = Mid(LETTERS, Int((26) * Rnd + 1), 1) & "     "
    ' Randomly generate the 2nd through 6th characters.
    ' Second letter
    intIndex = Int((5) * Rnd + 2)
    Do Until Mid$(strPassword, intIndex, 1) = " "
        intIndex = Int((5) * Rnd + 2)
    Loop
    strPassword = Left$(strPassword, intIndex - 1) & Mid(LETTERS, Int((26) * Rnd + 1), 1) & Mid$(strPassword, intIndex + 1)
    ' First symbol
    intIndex = Int((5) * Rnd + 2)
    Do Until Mid$(strPassword, intIndex, 1) = " "
        intIndex = Int((5) * Rnd + 2)
    Loop
    strPassword = Left$(strPassword, intIndex - 1) & Mid(SYMBOLS, Int((31) * Rnd + 1), 1) & Mid$(strPassword, intIndex + 1)
    Do Until Mid$(strPassword, intIndex, 1) = " "
        intIndex = Int((5) * Rnd + 2)
    Loop
    strPassword = Left$(strPassword, intIndex - 1) & Mid(SYMBOLS, Int((31) * Rnd + 1), 1) & Mid$(strPassword, intIndex + 1)
    ' First number
    Do Until Mid$(strPassword, intIndex, 1) = " "
        intIndex = Int((5) * Rnd + 2)
    Loop
    strPassword = Left$(strPassword, intIndex - 1) & Int((10) * Rnd) & Mid$(strPassword, intIndex + 1)
    ' Second number
    Do Until Mid$(strPassword, intIndex, 1) = " "
        intIndex = Int((5) * Rnd + 2)
    Loop
    strPassword = Left$(strPassword, intIndex - 1) & Int((10) * Rnd) & Mid$(strPassword, intIndex + 1)
    
    MsgBox strPassword

Open in new window

AleksAuthor Commented:
I understand "strPassword" is the variable with holding the value ?

I tried displaying the value on the screen with the code below but nothing showed.

<%=strPassword%>

Open in new window

Martin LissOlder than dirtCommented:
Yes.
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

AleksAuthor Commented:
I actually get this error:

Microsoft VBScript compilation  error '800a03f3'

Expected '='

/BlueDot/Intranet/Contacts/webaccess_SPadd.asp, line 94
Const LETTERS As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Martin LissOlder than dirtCommented:
Try changing lines 1 and 2 to
Const LETTERS  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Const SYMBOLS  = "~!@#$%^&*()_+-={}|[]\:"";'<>?,./"

Open in new window

Also and it's not important but I forgot a ' Second symbol comment between lines 22 and 23.
Martin LissOlder than dirtCommented:
If the "$" are a problem, remove them.
AleksAuthor Commented:
This is what I have and the error is below:

    Const LETTERS  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	Const SYMBOLS  = "~!@#$%^&*()_+-={}|[]\:"";'<>?,./"
    Dim strPassword As String
    Dim intIndex As Integer
    
    Randomize
    
    ' Generate the password string making the 1st character a letter
    strPassword = Mid(LETTERS, Int((26) * Rnd + 1), 1) & "     "
    ' Randomly generate the 2nd through 6th characters.
    ' Second letter
    intIndex = Int((5) * Rnd + 2)
    Do Until Mid$(strPassword, intIndex, 1) = " "
        intIndex = Int((5) * Rnd + 2)
    Loop
    strPassword = Left$(strPassword, intIndex - 1) & Mid(LETTERS, Int((26) * Rnd + 1), 1) & Mid$(strPassword, intIndex + 1)
    ' First symbol
    intIndex = Int((5) * Rnd + 2)
    Do Until Mid$(strPassword, intIndex, 1) = " "
        intIndex = Int((5) * Rnd + 2)
    Loop
    strPassword = Left$(strPassword, intIndex - 1) & Mid(SYMBOLS, Int((31) * Rnd + 1), 1) & Mid$(strPassword, intIndex + 1)
    Do Until Mid$(strPassword, intIndex, 1) = " "
        intIndex = Int((5) * Rnd + 2)
    Loop
    strPassword = Left$(strPassword, intIndex - 1) & Mid(SYMBOLS, Int((31) * Rnd + 1), 1) & Mid$(strPassword, intIndex + 1)
    ' First number
    Do Until Mid$(strPassword, intIndex, 1) = " "
        intIndex = Int((5) * Rnd + 2)
    Loop
    strPassword = Left$(strPassword, intIndex - 1) & Int((10) * Rnd) & Mid$(strPassword, intIndex + 1)
    ' Second number
    Do Until Mid$(strPassword, intIndex, 1) = " "
        intIndex = Int((5) * Rnd + 2)
    Loop
    strPassword = Left$(strPassword, intIndex - 1) & Int((10) * Rnd) & Mid$(strPassword, intIndex + 1)
    
    MsgBox strPassword

Open in new window


Error:

Microsoft VBScript compilation  error '800a0401'

Expected end of statement

/BlueDot/Intranet/Contacts/webaccess_SPadd.asp, line 96
Dim strPassword As String
----------------^
Martin LissOlder than dirtCommented:
Change line 3 and 4 to
    Dim strPassword
    Dim intIndex

Open in new window

AleksAuthor Commented:
Microsoft VBScript compilation  error '800a0408'

Invalid character

/BlueDot/Intranet/Contacts/webaccess_SPadd.asp, line 106
Do Until Mid$(strPassword, intIndex, 1) = " "
Martin LissOlder than dirtCommented:
As I suggested above, try removing all the the "$". They are in lines 13, 16, 19, 22, 23, 26, 28, 31 and 36.
AleksAuthor Commented:
Error on the last line:

Microsoft VBScript runtime  error '800a0046'

Permission denied: 'MsgBox'
AleksAuthor Commented:
I removed that line, but the password doesn't show anything. Here is my entire page, you can check it out.

<html>
<head>
<!--#include file="../../includes/bdot/scripts.asp"-->
<meta charset="utf-8">
<title></title>

<script language="vbscript" runat="server">
    Const LETTERS  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	Const SYMBOLS  = "~!@#$%^&*()_+-={}|[]\:"";'<>?,./"
    Dim strPassword
    Dim intIndex
    
    Randomize
    
    ' Generate the password string making the 1st character a letter
    strPassword = Mid(LETTERS, Int((26) * Rnd + 1), 1) & "     "
    ' Randomly generate the 2nd through 6th characters.
    ' Second letter
    intIndex = Int((5) * Rnd + 2)
    Do Until Mid(strPassword, intIndex, 1) = " "
        intIndex = Int((5) * Rnd + 2)
    Loop
    strPassword = Left(strPassword, intIndex - 1) & Mid(LETTERS, Int((26) * Rnd + 1), 1) & Mid(strPassword, intIndex + 1)
    ' First symbol
    intIndex = Int((5) * Rnd + 2)
    Do Until Mid(strPassword, intIndex, 1) = " "
        intIndex = Int((5) * Rnd + 2)
    Loop
    strPassword = Left(strPassword, intIndex - 1) & Mid(SYMBOLS, Int((31) * Rnd + 1), 1) & Mid(strPassword, intIndex + 1)
    Do Until Mid(strPassword, intIndex, 1) = " "
        intIndex = Int((5) * Rnd + 2)
    Loop
    strPassword = Left(strPassword, intIndex - 1) & Mid(SYMBOLS, Int((31) * Rnd + 1), 1) & Mid(strPassword, intIndex + 1)
    ' First number
    Do Until Mid(strPassword, intIndex, 1) = " "
        intIndex = Int((5) * Rnd + 2)
    Loop
    strPassword = Left(strPassword, intIndex - 1) & Int((10) * Rnd) & Mid(strPassword, intIndex + 1)
    ' Second number
    Do Until Mid(strPassword, intIndex, 1) = " "
        intIndex = Int((5) * Rnd + 2)
    Loop
    strPassword = Left(strPassword, intIndex - 1) & Int((10) * Rnd) & Mid(strPassword, intIndex + 1)
    
   
</script>
</head>

<body>
<p>password = <%=strPassword%></p>

</body>

Open in new window

Martin LissOlder than dirtCommented:
OK, just remove that line and do whatever you want with strPassword.
AleksAuthor Commented:
I tried to display it but it shows nothing.
Martin LissOlder than dirtCommented:
Please post your code.
AleksAuthor Commented:
I did, read above, I posted the whole page.
Martin LissOlder than dirtCommented:
I missed that, sorry.

I don't know anything about VBScript but the web shows me things like this to show the contents of a variable.

WScript.Echo strPassword

Try that at line 44 in your code.
AleksAuthor Commented:
Microsoft VBScript runtime  error '800a01a8'

Object required: 'WScript'
AleksAuthor Commented:
I also noticed the script is missing numbers  :$
It has letters and symbols.
Martin LissOlder than dirtCommented:
I'm sorry but because of my lack of VBScript knowledge it looks like you need someone else to help you.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AleksAuthor Commented:
Ok ... thanks. Ill post the points and open another ticket.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.