Solved

# Help - Quick random code check!

Posted on 2006-05-03
213 Views
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
;)
0
Question by:Kartibok

LVL 6

Expert Comment

Shouldn't it be

num2 = num1 + 1

HTH :)
0

LVL 44

Accepted Solution

Length = "qwertyqwerty"
'1. strange to have a string variable with a numeric name

AnsLen = Len(Length)
'2. in this example, this variable = 12
'3. since you are performing division on this number, you might consider what happens to the calcumations when AnsLen is not an even multiple of 3.

num1 = AnsLen / 3
'4. probably better to use num1 = AnsLen \ 3
'5. in this example, num1 = 4

num2 = num1 + num1
'6. in this example, num2 = 8

num3 = num2 + 1
'7. in this example, num3 = 9

'Randomize
'8. I don't know if you commented a Randomize statement or this is a comment.
'9. The following rand# sequence values will ALWAYS be seeded the same.  Everyone's character selection should be identical, not random, based on their generation from the start of the program.  The first passwords of every day will be identical, the second passwords of every day will be identical, etc.
'10. Beware that there are problems with VB's PRNG statements that will produce identical sequences far more often than you expect.

rand1 = Int((num1 * Rnd) + 1)
'11. in this example, first click rand1 will be 3

'Randomize
rand2 = Int((num2 * Rnd) + num1)
'12. in this example, first click rand1 will be 8

'Randomize
rand3 = Int((AnsLen * Rnd) + num3)
'13. in this example, first click rand1 will be 15

======================================
14. If using these rand# values in a Mid() function, you need to restrict their values to between 1 and AnsLen.  rand1 and rand2 calculations produce 'safe' values.  The rand3 calculation does not.
A 'safe' alternative:
rand3 = Int((AnsLen * Rnd) +1)

15. Are you really trying to choose  three random characters from three different segments of the string?  If so, use:
rand1 = Int(num1 * Rnd) + 1
rand2 = Int(num1 * Rnd) + 5   'or num1 +1
rand3 = Int(num1 * Rnd) + 9   'or (2*num1) +1
0

Author Comment

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.

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

****************************************
0

LVL 44

Expert Comment

@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
0

## Featured Post

### Suggested Solutions

Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…