-
Hi I am farely new to Visual Basics and i would like to make a program which generates 6 numbers out of 1-45.
None of the numbers must appear twice and it should just generate 6 random numbers out of 1-45
I have made a layout but dont really know any coding so if somone cn help me , pls contact me on
[email protected]
or on icq
44437708
-
Add a command button and a listbox to a form, then copy
the followin code:
Private Sub Command1_Click()
Dim intNumber As Integer
List1.Clear
Do While List1.ListCount < 6
intNumber = GenerateNumber(1, 45)
If Not IsMember(intNumber, List1) Then
List1.AddItem intNumber
End If
Loop
End Sub
Function GenerateNumber(ByVal intLow As Integer, _
ByVal intHigh As Integer) As Integer
Randomize
GenerateNumber = Int((intHigh * Rnd) + intLow)
End Function
Function IsMember(ByVal intNumber As Integer, lstBox As ListBox) As Boolean
Dim intIndex As Integer
For intIndex = 0 To lstBox.ListCount - 1
If CStr(intNumber) = lstBox.List(intIndex) Then
IsMember = True
End If
Next intIndex
End Function
Hope this helps
TheBao
-
I have mailed you my project I made, but basically I used the following:
Code:
Private Sub Command1_Click()
Dim BallNo As Integer, found As Boolean, collect As New Collection, i As Integer
On Error Resume Next
Do Until collect.Count = 6
BallNo = Int(Rnd * 45) + 1
collect.Add CStr(BallNo), CStr(BallNo)
found = (Err = 0)
If found Then
Err.Clear
Do While Not found = True
BallNo = Int(Rnd * 49) + 1
Loop
Else
collect.Add BallNo, BallNo
End If
Loop
For BallNo = 1 To collect.Count
Lbl(i).Caption = collect.Item(BallNo)
i = i + 1
Next
End Sub
Basically, I am using the array and collection objects to store the random numbers, then a loop to check and update the number if it's already taken.
I then have 6 labels on a form (all with the same name of lbl), the bottom for..next loop selects the label, puts one of the random numbers into it's cation property before selecting the next number, and next label to update.
-
try to make use of the Microsoft Script Runtime object (Distionary)
Take a look on my sample code on this Thread
Cheers!
-
Thanx Very Much Guys and thank you too Alex, i will ask u for further help because i am new to VB6 and dont have MSDN...
Or any VB6 books cuz they cost too much!!!!
-
Too right, I used the learning edition for about 2 years because of this!