Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Problem Comparing Randomly Generated Numbers...

clientclient Member Posts: 23
Hi,
I made this simple Virtual Lottery Game, everything works out fine except the comparision of the Randomly Generated Numbers with Numbers entered into the Text Boxes.
So here's the concept, the user fills in six Input Fields with numbers between 1 and 49 (here defined as Text1(0) - Text1(5) Control Array), the numbers generated randomly are displayed in the Labels defined as Label1(0) - Label1(5) Control Array, and to show the numbers that match I deceided to put another Label Control Array named as Label2(0) Label2(5) which main purpose is to simply show a "+" below the matching number.
So here's the problem, I tried every possible Loop but it still doesn't show the "+" below the matching numbers. I tried to define the variables as static and integer but I'm still not smarter than I was before.
I have pasted the code below :(

[code]Private Sub Command1_Click()
Static i As Integer
Static j As Integer
Static k As Integer
Static l As Integer
Static random As Integer

For i = 0 To 5
If (Val(Text1(i).Text) >= 1 And Val(Text1(i).Text) <= 49) Then
For j = 0 To 5
Randomize
random = Int((Rnd() * 49) + 1)
Label1(j).Caption = random
'This is where my problems start... :(
For k = 0 To 5
If (Val(Text1(j).Text) = Val(Label1(j).Caption)) Then
Label2(j).Caption = "+"
End If
Next k
Next j
Else
MsgBox ("Not Valid " &i)
End If
Next i
End Sub[/code]

Thanks in advance...

Comments

  • infidelinfidel Member Posts: 2,900
    [b][red]This message was edited by infidel at 2004-3-25 8:15:2[/red][/b][hr]
    : Hi,
    : I made this simple Virtual Lottery Game, everything works out fine except the comparision of the Randomly Generated Numbers with Numbers entered into the Text Boxes.
    : So here's the concept, the user fills in six Input Fields with numbers between 1 and 49 (here defined as Text1(0) - Text1(5) Control Array), the numbers generated randomly are displayed in the Labels defined as Label1(0) - Label1(5) Control Array, and to show the numbers that match I deceided to put another Label Control Array named as Label2(0) Label2(5) which main purpose is to simply show a "+" below the matching number.
    : So here's the problem, I tried every possible Loop but it still doesn't show the "+" below the matching numbers. I tried to define the variables as static and integer but I'm still not smarter than I was before.
    : I have pasted the code below :(
    :
    : [code]Private Sub Command1_Click()
    : Static i As Integer
    : Static j As Integer
    : Static k As Integer
    : Static l As Integer
    : Static random As Integer
    :
    : For i = 0 To 5
    : If (Val(Text1(i).Text) >= 1 And Val(Text1(i).Text) <= 49) Then
    : For j = 0 To 5
    : Randomize
    : random = Int((Rnd() * 49) + 1)
    : Label1(j).Caption = random
    : 'This is where my problems start... :(
    : For k = 0 To 5
    : If (Val(Text1(j).Text) = Val(Label1(j).Caption)) Then
    : Label2(j).Caption = "+"
    : End If
    : Next k
    : Next j
    : Else
    : MsgBox ("Not Valid " &i)
    : End If
    : Next i
    : End Sub[/code]
    :
    : Thanks in advance...

    Dude, your problem is your loops. You've got a for loop in a for loop in a for loop. That means the inner loop, the one with k as the index is going to execute 25 times, meaning the code within the k loop is going to be hit 125 times. You need to un-nest your loops. In fact, if you validate the values entered by the user in the Text1_Validate event, you can focus solely on the random numbers and comparison

    [code]
    For i = 0 To 5
    'generate a random number
    'if it matches the number the user entered, mark it with a +
    Next
    [/code]

    And don't use static variables like this.

    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

    [code]
    $ select * from users where clue > 0
    no rows returned
    [/code]



Sign In or Register to comment.