# Random Array

• 02-24-2005, 08:57 PM
XRsTX
Random Array
Ok I'm trying to create a multiplier where you can press a command button and it Randomizes to numbers and multiplies it.

Code:

```Private Sub cmdrandom_Click() Dim MAX As Integer Dim MIN As Integer For MAX = 1 To 200 For MIN = 1 To 200 If cmdrandom.CausesValidation = True Then Dim x As Integer Dim y As Integer Next x = txtcode.Text y = TxtCode1.Text x = MAX y = MIN txtdone.Text = y * x End If End Sub```
it keeps saying "COMPILE ERROR:"
"NEXT WITHOUT FOR"
• 02-25-2005, 01:21 AM
Phil Weber
You have two For... statements, but only one Next statement. You need a Next for each For.

Also, your Next statement is within an If...Then block that's within one of the For... loops, which won't work. Your code should look like this:
Code:

```For MAX = 1 To 200     For MIN = 1 To 200         If cmdRandom.CausesValidation Then             ' Do something         End If     Next MIN Next MAX```
Finally, the code in the inner loop will execute 40,000 times (200 * 200). It's unlikely that cmdRandom's CausesValidation property will change inside the loop, so you should probably test it once before entering the loops, rather than testing it 40,000 times. ;)
Code:

```If cmdRandom.CausesValidation Then     For MAX = 1 To 200         For MIN = 1 To 200             ' Do something         Next MIN     Next MAX End If```
• 02-25-2005, 07:38 PM
XRsTX
Quote:

Originally Posted by Phil Weber
You have two For... statements, but only one Next statement. You need a Next for each For.

Also, your Next statement is within an If...Then block that's within one of the For... loops, which won't work. Your code should look like this:
Code:

```For MAX = 1 To 200     For MIN = 1 To 200         If cmdRandom.CausesValidation Then             ' Do something         End If     Next MIN Next MAX```
Finally, the code in the inner loop will execute 40,000 times (200 * 200). It's unlikely that cmdRandom's CausesValidation property will change inside the loop, so you should probably test it once before entering the loops, rather than testing it 40,000 times. ;)
Code:

```If cmdRandom.CausesValidation Then     For MAX = 1 To 200         For MIN = 1 To 200             ' Do something         Next MIN     Next MAX End If```

That Worked Perfect thanks...But, How Do I make it choose any number out of the 1 to 200?
• 02-25-2005, 08:48 PM
mstraf
your code is really confusing, because before you set X and Y from two text boxes, then just after that you assign MIN and MAX to them. What's the point of using the text boxes if their values are not used?
To generate a random number between 1 and 200:

1+ fix(rnd * 199)

Marco
• 02-25-2005, 09:29 PM
XRsTX
Yeah I just figured that i didnt need the 2 integers...Oh yeah thanks for that code..but i had to fix it to this: 1 To fix(rnd * 180). Thanks alot Mstraf and Phil!
• 02-28-2005, 01:15 PM
mstraf
one TO fix?!?!?!
Are you using a random number for your exit loop? I had the idea that you wanted to choose just a random number...
Marco
• 02-28-2005, 01:43 PM
XRsTX
1 To fix(rnd * 180) is choosing random number for me...here try it yourself:

Private Sub CmdRandom_Click()
Dim MAX As Integer
Dim MIN As Integer
For MAX = 1 To Fix(Rnd * 180)
For MIN = 1 To 1 + Fix(Rnd * 180)
If CmdRandom.CausesValidation Then
txtcode.Text = MIN
TxtCode1.Text = MAX
txtdone.Text = MIN * MAX
End If
Next MIN
Next MAX
If CmdRandom.CausesValidation Then
For MAX = 1 To Fix(Rnd * 180)
For MIN = 1 To Fix(Rnd * 180)
Next MIN
Next MAX
End If
End Sub
• 02-28-2005, 02:02 PM
mstraf
Did you at least try to debug your code? Put a break at the first for statement, run and go step by step using F8....
The first two nested for statements loop a lot of times (set by two random numbers) but in the end in txtdone what you see is the results of only the last loop.
What about the completely useless two nested loops inside the last if statement, that don't do anything at all?
Sorry, but I am still trying to find out what you want to accomplish...
If you want to click in a button and multiply two random numbers, this is my code (not tested):

private sub command1_click()
txtcode.Text = 1 + fix(rnd * 200)
txtcode1.Text = 1 + fix(rnd * 200)
txtdone.text = txtcode.text * txtcode1.text
end sub

Marco