Random Array

 DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

#### Hybrid View

1. Registered User
Join Date
Feb 2005
Posts
103

## 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"

2. Senior Member
Join Date
Nov 2003
Location
Portland, OR
Posts
8,387
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```
Last edited by Phil Weber; 02-25-2005 at 01:26 AM.

3. Registered User
Join Date
Feb 2005
Posts
103
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?

4. 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

5. Registered User
Join Date
Feb 2005
Posts
103
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!

6. 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

7. Registered User
Join Date
Feb 2005
Posts
103
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

8. 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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise