Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 301
  • Last Modified:

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
;)
0
Kartibok
Asked:
Kartibok
  • 2
1 Solution
 
cubixSoftwareCommented:
Shouldn't it be

num2 = num1 + 1

HTH :)
0
 
aikimarkCommented:
Code with my comments:

    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.
Note: for more information, read http://www.15seconds.com/issue/051110.htm

    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
 
KartibokAuthor Commented:
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

****************************************
0
 
aikimarkCommented:
@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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now