06-18-2004, 05:01 PM
I am making a simple Password Generator which will pick four mixed case letters, and four numbers, scramble them, then present it to the user for use as a password. Also if a number or a letter is used, one in the password, it won't be used again.
The problem I am having, is that when the "Generate" Button is click, the result is eight of the same letters or numbers, and I can't seem to figure out why it's doing it, and how I could correct it.
06-18-2004, 06:16 PM
You might consider using a byte array and the StrConv function to generate your characters for you. The code you had was quite redundant in converting strings to integers and back... I did not really follow your code to find out why it was generating only one character. Try this function and see if it might work for you:
Private Function MakePassword() As String
'Make a string of random characters.
'On average, return 4 random numbers and 4 random lower and upper case letters
'Return: a random 8 character string.
Dim b(7) As Byte
Dim i As Long, t As Long
For i = 0 To 7
'Generate a random byte.
If Rnd > 0.5 Then
'Generate a ASCII code for a letter.
If Rnd > 0.5 Then
b(i) = Int(26 * Rnd + 65)
b(i) = Int(26 * Rnd + 97)
'Generate a ASCII code for a number.
b(i) = Int(10 * Rnd + 48)
'Check the byte for duplicates.
For t = 0 To i - 1
If b(i) = b(t) Then
i = i - 1
'Assign the result to the return converting the byte array
'into a string.
MakePassword = StrConv(b(), vbUnicode)
Two other notes to consider are always declare each of your variables and you can insert the randomize statement in your load subroutine to randomize the return of the Rnd function.
Private Sub Form_Load()
'Seed the random number generator using the sytem timer for a seed.
'This is bad declaration practice as only the last variable is a string.
'The other variables are variants:
Dim s1, s2, s3 As String
Dim S1 As String, S2 As String, S3 As String
06-18-2004, 06:22 PM
You have a lot of redundant code in the project:
Private Function PickLetter()
'Generate a Random Number to pick a Letter
Dim Random1, Random2, x As Integer
Random1 = Int(Rnd(-Timer) * 26) + 1
Random2 = Int(Rnd() * 26) + 1
mstrLetter = Int(Random1 + Random2) / 2
mstrLetter = Int(Rnd() * 100)
mstrLetter = mstrLetter Mod 26
Random1 = Int(Rnd(-Timer) * 1) + 1
Random2 = Int(Rnd() * 1) + 1
x = Int(Random1 + Random2) / 2
x = Int(Rnd() * 10)
x = x Mod 2
If x <> 1 Then mstrLetter = Chr(mstrLetter + 65) Else mstrLetter = Chr(mstrLetter + 97)
The bold lines are the only lines that work toward the result. The other values are trashed.
So, I removed:
Random1 = Int(Rnd(-Timer) * 8) + 1
Random2 = Int(Rnd() * 8) + 1
mintIndex = Int(Random1 + Random2) / 2
From your Indexing procedure (and series of lines that look like this in the PickNumber and PickLetter, and it worked)
06-18-2004, 06:38 PM
Thanks alot for the help. As for the redundant code, I'm a little rusty and forgetful. I learnt VB for about 6 weeks last year, then I haven't had to use it for about eight months, now I'm using it to make a couple of "pet" projects for friensd to share.
06-21-2004, 05:49 AM
When I debug the codes above,I found that sometimes I can't get a right answer(maybe 5 letters and 3 numbers or other).
I write an inhanced sub with collection,as follow:
Private Sub Command1_Click()
'makepassword 'default result,i.e what Zemzerrett wants.
makepassword 7, 10 ' get a combination with 7 mixed case letters and 10 numbers
Sub makepassword(Optional ByVal letternum As Integer = 4, Optional ByVal numbernum As Integer = 4, Optional ByRef password As String)
Dim LETTER As New Collection, NUMBER As New Collection, RESULT As New Collection, I As Integer, TEMP As Integer, a() As String
ReDim a(1 To letternum + numbernum)
For I = 0 To 25
LETTER.Add Chr(I + 65)
LETTER.Add Chr(I + 97)
If I < 10 Then NUMBER.Add I
For I = 1 To letternum
TEMP = Int(Rnd * LETTER.Count + 1)
For I = 1 To numbernum
TEMP = Int(Rnd * NUMBER.Count + 1)
For I = 1 To letternum + numbernum
TEMP = Int(Rnd * RESULT.Count + 1)
a(I) = RESULT(TEMP)
Set LETTER = Nothing
Set NUMBER = Nothing
Set RESULT = Nothing
password = Join(a, "")