Password Program

jdjd1118

Member
Joined
Nov 30, 2005
Messages
12
Programming Experience
Beginner
I wrote a simple program to generate random passwords. This program works by first choosing whether the character is a number or letter, then chooses what character and continues until there are enough characters. Everything works fine, but when run the program usually spits out the same exact character 6 times. When run step by step in Debug, the program works perfectly, choosing 6 random characters. Any ideas how to fix this problem?
Thanks in advance.
 
Are you able to post the routine that generates your password strings? I'm sure we can help catch the error if we see the details.
 
Here is the routine:

VB.NET:
Do Until intCount = 6  'This is the loop the program will go into until the password is 6 characters long

    'First, the program will pick whether the character is a number or letter
    intRandom = rand.Next(1, 3)

    intLetter = 0

    'Here, we need to choose a letter
    Select Case intRandom
        Case 1
            Dim rand2 As New Random 'This new random will decide the numerical value that will decide the letter


            intLetter = rand2.Next(1, 26)

            Select Case intLetter
                Case 1 'intletter = 1, letter is A, 2 letter = B Etc....
                    strPassword += "A"
                Case 2
                    strPassword += "B"
                Case 3
                    strPassword += "C"
                Case 4
                    strPassword += "D"
                Case 5
                    strPassword += "E"
                Case 6
                    strPassword += "F"
                Case 7
                    strPassword += "G"
                Case 8
                    strPassword += "H"
                Case 9
                    strPassword += "I"
                Case 10
                    strPassword += "J"
                Case 11
                    strPassword += "K"
                Case 12
                    strPassword += "L"
                Case 13
                    strPassword += "M"
                Case 14
                    strPassword += "N"
                Case 15
                    strPassword += "O"
                Case 16
                    strPassword += "P"
                Case 17
                    strPassword += "Q"
                Case 18
                    strPassword += "R"
                Case 19
                    strPassword += "S"
                Case 20
                    strPassword += "T"
                Case 21
                    strPassword += "U"
                Case 22
                    strPassword += "V"
                Case 23
                    strPassword += "W"
                Case 24
                    strPassword += "X"
                Case 25
                    strPassword += "Y"
                Case 26
                    strPassword += "Z"
            End Select

            'if intRandom is 2, the character will be a number
        Case 2
            Dim Rand3 As New Random 'This will create a random number for the password
            Dim strNumber As String 'This will store the random number to be used in the Password 

            strNumber = Rand3.Next(0, 9)

            strPassword = strPassword + strNumber
    End Select
Loop
 
I think you are over complicating your approach a bit. Check this code out and let me know what you think. Should achive what you are after.

VB.NET:
Module Module1

    Sub Main()

        'Array of Alpha Characters for the random selection by index
        Dim myAlphabet() As String = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
        'Array of Numerics for the random selection by index
        Dim myNumerics() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
        'Random number generator (you only need one of these)
        Dim myRandomGenerator As Random = New Random()

        Dim myPasswordString As String = ""
        Do Until myPasswordString.Length = 6
            Select Case myRandomGenerator.Next(2)
                Case 0  'Choose a letter
                    myPasswordString = String.Format("{0}{1}", myPasswordString, myAlphabet(myRandomGenerator.Next(26))) 'Chooses a random index from the alpha array, 1-26
                Case 1  'Choose a number
                    myPasswordString = String.Format("{0}{1}", myPasswordString, myNumerics(myRandomGenerator.Next(10))) 'Chooses a random index from the numberics, 1-0
            End Select
        Loop

        Console.WriteLine(myPasswordString)
            

    End Sub

End Module

And remember, while working code is good, understanding WHY it works is better. Feel free to ask questions even if this works for you but you are not sure why!
 
Back
Top