-
Apr 15th, 2010, 08:49 PM
#1
Thread Starter
New Member
Counter to count # of loops
Ok, so I basically have 3 labels, that each generate a unique random number between 1 to 5, when I click on the command box. I need the the 4th label to show the number of loops it took to get the 3 unique numbers. I'm stuck.
-
Apr 15th, 2010, 08:52 PM
#2
Re: Counter to count # of loops
Loops? How are you determining random nubers in a loop?
-
Apr 15th, 2010, 09:03 PM
#3
Re: Counter to count # of loops
baja vu's question aside, put a counter in the loop, increment it by one.
-
Apr 16th, 2010, 12:44 AM
#4
Re: Counter to count # of loops
Welcome to the forums...
Here's an example:
Code:
Option Explicit
Dim num(2) As Integer
'~~~ Function for generating Random Number within a range
Private Function RandomInteger(Lowerbound As Integer, Upperbound As Integer) As Integer
RandomInteger = Int((Upperbound - Lowerbound + 1) * Rnd + Lowerbound)
End Function
Private Sub Command1_Click()
Randomize
Dim i As Integer
Dim Temp As Integer
Dim intCounter As Integer
For i = 0 To 2 '~~~ Initial value will be 0
num(i) = 0
Next i
For i = 0 To 2
Do
Temp = RandomInteger(1, 5) '~~~ Random number generation
intCounter = intCounter + 1 '~~~ Counter
Loop Until UniqueNum(Temp) = True '~~~ Check if it's a unique number
num(i) = Temp '~~~ Saving the number
Next i
'~~~ Finally displaying it in the LabelBoxes
Label1.Caption = num(0)
Label2.Caption = num(1)
Label3.Caption = num(2)
MsgBox intCounter '~~~~ Display the number of loops
End Sub
'~~~ Function to check whether it's unique or not
Private Function UniqueNum(ByVal number As Integer) As Boolean
Dim i As Integer
For i = 0 To 2
If num(i) = number Then
UniqueNum = False
Exit Function
End If
Next i
UniqueNum = True
End Function
.....
If my post was helpful to you, then express your gratitude using Rate this Post.
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|