Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

random number

Status
Not open for further replies.

akutty

IS-IT--Management
Jul 7, 2004
31
GB
Hello, was trying to create random password generator.
First time when it runs it generates password, but next time also it generates same set of password. Seems there is some problem with the random no generation part. It always generates the same sequence
pls help
Anil

Private Const LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
Private Sub Form_Load()
Passwordgenerator
End Sub

Function Passwordgenerator()

Dim Passwordarray(7)
For i = 1 To 7
Passwordarray(i) = ""
Next
For i = 0 To 3
Label1(i).Caption = ""
Next

i = 1
'put this in a command button

'Add a label and name it label1,change its caption to "", set auto size to true

On Error Resume Next

For i = 0 To 4
b = 0
Do
DoEvents
b = b + 1
Debug.Print Int(Rnd * 26) + 1
a = Int(52 * Rnd)
ltr = Mid(LETTERS, a, 1) ' does the actual random generation
ltr2 = Asc(ltr) 'Numbervalue of the random Char
Select Case b
Case 2
Randomnumber = Int(Rnd * 10)
If Randomnumber <= 2 Then
Passwordarray(b) = Trim(Chr(64))
ElseIf Randomnumber >= 3 And Randomnumber <= 5 Then
Passwordarray(b) = Trim(Chr(37))
ElseIf Randomnumber >= 6 And Randomnumber <= 8 Then
Passwordarray(b) = Trim(Chr(36))
Else
Passwordarray(b) = Trim(Chr(95))
End If


Case 5
Passwordarray(b) = Trim(Str(ltr2))

Case 7
Passwordarray(b) = Trim(Str(ltr2))
Case Else
Passwordarray(b) = Chr(ltr2)
End Select
' Label1.Caption = Label1.Caption + Chr(ltr2) ' character value shown

Loop Until b = 8 ' The number of characters
Label1(i).Caption = Passwordarray(1) + Passwordarray(2) + Passwordarray(3) + Passwordarray(4) + Passwordarray(5) + Passwordarray(6)



Next

'Label1(1).Caption = Passwordarray(1)
'Label1(2).Caption = Passwordarray(1)
'Label1(3).Caption = Passwordarray(1)

End Function

 
At the top of your script run randomize() which should solve any problems
 
Try using the Randomize statement at the beginning of the procedure. This will properly seed the Rnd function.
Code:
...
On Error Resume Next

Randomize 'seed the Rnd function
For i = 0 To 4
b = 0
Do
...
You can look it up in the help files for more information.

zemp
 
It is probably just my own simplistic view, but the routine seems a bit much for the results. I "simplified" it a bit, and see no pratical difference in the gibberish generated:


Code:
Function basPswordgen(Optional PwdLen As Integer = 7) As String

    'Purpose:   Generate a Random Character String of the specified Length
    'Michael Red    8/9/2004

    Const LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()+?/>.<,~`"

    Dim Jdx As Integer          'PwdAry Index (Specific Char in Pwd)
    Dim Kdx As Integer          'Pointer into the Letters Array
    Dim Ltr As String * 1
    Dim RndNum As Integer
    Dim MyPwd As String

    On Error Resume Next                'Invoke Generic Error Handling

    Randomize

    While Jdx <= PwdLen

        Kdx = Int(71 * Rnd)                 'actual random generation
        Ltr = Mid(LETTERS, Kdx, 1)          'Get the Letter Assigned
        MyPwd = MyPwd & Ltr                 'Add Letter To PWD
        Jdx = Jdx + 1                       'Increment COunter for Chars

    Wend

    basPswordgen = MyPwd

End Function

Of course, part of the simplification was to do away with the reference to controls (and all that messy stuuuuffffff re forms) which is probably necessary, and I made the procedure return the gibberish string (could return an Array or strings if there is some reason for the multiple generation of values).


MichaelRed
mlred@verizon.net

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top