Hi all, I'm new to VB and I am doing a lottery program.

I managed to produce 7 random numbers but I am stuck with the sorting. All I can think of is to use if statements to do comparsion, but that will be a long way to do it and is very stupid if I do that .

I understand that there are bubble sorting, but I am not sure how to implement it.
I have another one which is to store the 7 numbers into 7 labels. And it's similar as below (just that a1 replace with Labal1.Caption and etc)

I will appreciate if you can help. Thanks.

``````Dim a1, a2, a3, a4, a5, a6, a7 As Integer

Private Sub cmdClick_Click()

cmdClick.Enabled = False
Sleep 2000 ' wait for 2 seconds.

a1 = Random(1, 45)

a2 = Random(1, 45)
If (a2 = a1) Then
a2 = Random(1, 45)
End If

a3 = Random(1, 45)
If (a3 = a2) Or (a3 = a1) Then
a3 = Random(1, 45)
End If

a4 = Random(1, 45)
If (a4 = a2) Or (a4 = a1) Or (a4 = a3) Then
a4 = Random(1, 45)
End If

a5 = Random(1, 45)
If (a5 = a4) Or (a5 = a3) Or (a5 = a2) Or (a5 = a1) Then
a5 = Random(1, 45)
End If

a6 = Random(1, 45)
If (a6 = a5) Or (a6 = a4) Or (a6 = a3) Or (a6 = a2) Or (a6 = a1) Then
a6 = Random(1, 45)
End If

a7 = Random(1, 45)
If (a7 = a6) Or (a7 = a5) Or (a7 = a4) Or (a7 = a3) Or (a7 = a2) Or (a7 = a1) Then
a7 = Random(1, 45)
End If

Text1.Text = a1 & " " & a2 & " " & a3 & " " & a4 & " " _
& a5 & " " & a6 & " " & a7``````

This is something that I can think of, but I know it will be tedious and not good if I continue.

``````Dim temp As Integer

If (a2 < a1) Then 'if a2 is smaller than a1
temp = a2   'store a2 into temp
a2 = a1     'a2 change to a1
a1 = temp   'a1 replace with a2(first number) = temp
End If

'continue to compare other variables with a1
'restart comparison then move on to a2 till a7.``````

I did it!

I used two loops and managed to product the results in ascending order.

Serene Joy,
I am learning visual basic too and also created a Lottery program. What version of visual basic are you using?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.