Kartibok
asked on
Help - Quick random code check!
Hi all,
The code below is designed to pick up three letters from a users special word. Its similar to the way banks deal with their passwords.
I have split the word into three so when I randomise it I don't get the same letters or numbers.
It seems to work fine but when it gets to the second or third figure it goes over the total figure in the special word.
Private Sub Command1_Click()
Dim num1 As Integer
Dim num2 As Integer
Dim num3 As Integer
Dim num4 As Integer
Dim rand1 As Integer
Dim rand2 As Integer
Dim rand3 As Integer
Dim AnsLen As Integer
Dim Length As String
Length = "qwertyqwerty"
AnsLen = Len(Length)
num1 = AnsLen / 3
num2 = num1 + num1
num3 = num2 + 1
'Randomize
rand1 = Int((num1 * Rnd) + 1)
'Randomize
rand2 = Int((num2 * Rnd) + num1)
'Randomize
rand3 = Int((AnsLen * Rnd) + num3)
MsgBox AnsLen
MsgBox rand1
MsgBox rand2
MsgBox rand3
End Sub
As you can see I am looking to randomly generate three integers that reflects the character location in the special word.
Any help is greatly appeciated.
K
;)
The code below is designed to pick up three letters from a users special word. Its similar to the way banks deal with their passwords.
I have split the word into three so when I randomise it I don't get the same letters or numbers.
It seems to work fine but when it gets to the second or third figure it goes over the total figure in the special word.
Private Sub Command1_Click()
Dim num1 As Integer
Dim num2 As Integer
Dim num3 As Integer
Dim num4 As Integer
Dim rand1 As Integer
Dim rand2 As Integer
Dim rand3 As Integer
Dim AnsLen As Integer
Dim Length As String
Length = "qwertyqwerty"
AnsLen = Len(Length)
num1 = AnsLen / 3
num2 = num1 + num1
num3 = num2 + 1
'Randomize
rand1 = Int((num1 * Rnd) + 1)
'Randomize
rand2 = Int((num2 * Rnd) + num1)
'Randomize
rand3 = Int((AnsLen * Rnd) + num3)
MsgBox AnsLen
MsgBox rand1
MsgBox rand2
MsgBox rand3
End Sub
As you can see I am looking to randomly generate three integers that reflects the character location in the special word.
Any help is greatly appeciated.
K
;)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Aikimark,
Many thanks for the help. I missed a few things out. The actual string would always be different, therefore the numbers would also be different.
In the end we decided to go with a random selection from the string and then reorder them, similar to the way a bank asks security questions.
I have included the code as we have it now and will use it this way.
Many thanks for your help.
K
;)
************************** ********** *
Dim rand1
Dim rand2
Dim rand3
Dim AnsLen
Dim aString
Dim tempNo
aString = "qwertyquerty"
AnsLen = Len(aString)
Randomize
rand1 = Int((AnsLen * Rnd) + 1)
Randomize
rand2 = Int((AnsLen * Rnd) + 1)
If rand2 = rand1 Then
While rand2 = rand1
Randomize
rand2 = Int((AnsLen * Rnd) + 1)
Wend
End If
Randomize
rand3 = Int((AnsLen * Rnd) + 1)
If (rand3 = rand2) Or (rand3 = rand1) Then
While (rand3 = rand2) Or (rand3 = rand1)
Randomize
rand3 = Int((AnsLen * Rnd) + 1)
Wend
End If
MsgBox rand1
MsgBox rand2
MsgBox rand3
If rand1 > rand2 Then
tempNo = rand2
rand2 = rand1
rand1 = tempNo
End If
If rand2 > rand3 Then
tempNo = rand3
rand3 = rand2
rand2 = tempNo
End If
If rand1 > rand2 Then
tempNo = rand2
rand2 = rand1
rand1 = tempNo
End If
MsgBox rand1
MsgBox rand2
MsgBox rand3
************************** ********** ****
Many thanks for the help. I missed a few things out. The actual string would always be different, therefore the numbers would also be different.
In the end we decided to go with a random selection from the string and then reorder them, similar to the way a bank asks security questions.
I have included the code as we have it now and will use it this way.
Many thanks for your help.
K
;)
**************************
Dim rand1
Dim rand2
Dim rand3
Dim AnsLen
Dim aString
Dim tempNo
aString = "qwertyquerty"
AnsLen = Len(aString)
Randomize
rand1 = Int((AnsLen * Rnd) + 1)
Randomize
rand2 = Int((AnsLen * Rnd) + 1)
If rand2 = rand1 Then
While rand2 = rand1
Randomize
rand2 = Int((AnsLen * Rnd) + 1)
Wend
End If
Randomize
rand3 = Int((AnsLen * Rnd) + 1)
If (rand3 = rand2) Or (rand3 = rand1) Then
While (rand3 = rand2) Or (rand3 = rand1)
Randomize
rand3 = Int((AnsLen * Rnd) + 1)
Wend
End If
MsgBox rand1
MsgBox rand2
MsgBox rand3
If rand1 > rand2 Then
tempNo = rand2
rand2 = rand1
rand1 = tempNo
End If
If rand2 > rand3 Then
tempNo = rand3
rand3 = rand2
rand2 = tempNo
End If
If rand1 > rand2 Then
tempNo = rand2
rand2 = rand1
rand1 = tempNo
End If
MsgBox rand1
MsgBox rand2
MsgBox rand3
**************************
@Kartibok
Thanks for the points. Glad I could help.
Did you read the article I linked in comment #10? Your use of the Randomize statement leads me to think that you don't understand how it works.
As I see your code, its purpose is to generate three distinct random integer numbers, between 1 and the length of some string, and then sort (order) the numbers sequentially.
I might suggest the following that would add a bit more variation on a daily basis and much better performance. This is taken from the follow-up article to the one I linked you to.
Dim rand1 As Integer 'avoid variants if you can
Dim rand2 As Integer
Dim rand3 As Integer
Dim AnsLen As Long 'I'm going to use this variable in a loop
Dim Length As String
Rnd -1
Randomize
For AnsLen = 1 To CLng(Date)
Rnd
Next
aString = "qwertyquerty"
AnsLen = Len(aString)
'Note: you should validate that AnsLen>2,
' else you won't be able to find three numbers in the range
rand1 = Int(AnsLen * Rnd) + 1
Do
rand2 = Int(AnsLen * Rnd) + 1
Loop Until rand1 <> rand2
Do
rand3 = Int(AnsLen * Rnd) + 1
Loop Until (rand1 <> rand3) And (rand2 <> rand3)
MsgBox "rand1=" & rand1 & vbcr & "rand2=" & rand2 & vbcr & "rand3=" & rand3
'Note: you can also use Debug.Print for such diagnostics
If rand1 > rand2 Then
tempNo = rand2
rand2 = rand1
rand1 = tempNo
End If
If rand2 > rand3 Then
tempNo = rand3
rand3 = rand2
rand2 = tempNo
End If
If rand1 > rand2 Then
tempNo = rand2
rand2 = rand1
rand1 = tempNo
End If
MsgBox "rand1=" & rand1 & vbcr & "rand2=" & rand2 & vbcr & "rand3=" & rand3
'Note: you can also use Debug.Print for such diagnostics
Thanks for the points. Glad I could help.
Did you read the article I linked in comment #10? Your use of the Randomize statement leads me to think that you don't understand how it works.
As I see your code, its purpose is to generate three distinct random integer numbers, between 1 and the length of some string, and then sort (order) the numbers sequentially.
I might suggest the following that would add a bit more variation on a daily basis and much better performance. This is taken from the follow-up article to the one I linked you to.
Dim rand1 As Integer 'avoid variants if you can
Dim rand2 As Integer
Dim rand3 As Integer
Dim AnsLen As Long 'I'm going to use this variable in a loop
Dim Length As String
Rnd -1
Randomize
For AnsLen = 1 To CLng(Date)
Rnd
Next
aString = "qwertyquerty"
AnsLen = Len(aString)
'Note: you should validate that AnsLen>2,
' else you won't be able to find three numbers in the range
rand1 = Int(AnsLen * Rnd) + 1
Do
rand2 = Int(AnsLen * Rnd) + 1
Loop Until rand1 <> rand2
Do
rand3 = Int(AnsLen * Rnd) + 1
Loop Until (rand1 <> rand3) And (rand2 <> rand3)
MsgBox "rand1=" & rand1 & vbcr & "rand2=" & rand2 & vbcr & "rand3=" & rand3
'Note: you can also use Debug.Print for such diagnostics
If rand1 > rand2 Then
tempNo = rand2
rand2 = rand1
rand1 = tempNo
End If
If rand2 > rand3 Then
tempNo = rand3
rand3 = rand2
rand2 = tempNo
End If
If rand1 > rand2 Then
tempNo = rand2
rand2 = rand1
rand1 = tempNo
End If
MsgBox "rand1=" & rand1 & vbcr & "rand2=" & rand2 & vbcr & "rand3=" & rand3
'Note: you can also use Debug.Print for such diagnostics
num2 = num1 + 1
HTH :)