Question

h3llk1ng
12-25-2008, 07:17 AM
I'm trying to make lottery program. I write this codes as now:

Private Sub Command1_Click()
Dim loto(1 To 6) As Integer
Dim i As Integer

For i = 1 To 6
Randomize
loto(i) = CInt(Rnd() * 43) + 6
Label1.Caption = loto(i)
Label2.Caption = loto(i)
Next i

I can't figure out how to print Label2, Label3 ... 6 different numbers? With this i can only print one number on Label1.caption. Label2.caption will be the same as label1.caption.:confused:

Thanks already.:chuckle:

PeetSoft
12-25-2008, 07:47 AM
In your for/next loop, label1 and 2 are overwritten after each next i.

Try this


For i = 1 To 6
Randomize
loto(i) = CInt(Rnd() * 43) + 6
Label1.Caption = label1.caption & ","& loto(i)
Label2.Caption = label2.caption & ","& loto(i)
Next i

h3llk1ng
12-25-2008, 08:20 AM
Thanks. One more question:

How can i show them ascending order in label captions.

The lowest number showed in label1. The higgest number in label6.caption? Any ideas

mkaras
12-25-2008, 09:35 AM
Do this. Put the six numbers into your array within the loop. Skip the work with the label captions in this first loop. Next step in the program is to sort the items in the array. You can write this yourself using a very simple pair of nested loops doing what is called a Bubble Sort. Lastly then assign the size numbers from the array to the label captions. If you make the labels a control array you can use a loop to assign the values from the array to the label captions.

I'll leave it to you to search either here in the forum or on the web for information on the Bubble Sort. There is a plethora of such information that can be found and so reduncant to duplicate it here. You can also search here for more information about control arrays in the case you are not familiar with them.

- mkaras

h3llk1ng
12-25-2008, 10:42 AM
First of all thanks for replies guys,

Private Sub Command1_Click()
Dim loto(1 To 6) As Integer
Dim a As Integer, b As Integer, i As Integer

Randomize
loto(1) = CInt(Rnd() * 43) + 6
loto(2) = CInt(Rnd() * 43) + 6
loto(3) = CInt(Rnd() * 43) + 6
loto(4) = CInt(Rnd() * 43) + 6
loto(5) = CInt(Rnd() * 43) + 6
loto(6) = CInt(Rnd() * 43) + 6


Label1.Caption = loto(1)
Label2.Caption = loto(2)
Label3.Caption = loto(3)
Label4.Caption = loto(4)
Label5.Caption = loto(5)
Label6.Caption = loto(6)

I wrote this code. Altough i read this http://www.xtremevbtalk.com/showthread.php?t=78889 and i didn't understand anything about Bubble Sort. Can u guys help me to ascending these arrays.

h3llk1ng
12-25-2008, 01:42 PM
Option Explicit
Dim loto(1 To 6) As Integer

Private Sub Command1_Click()

Dim a As Integer, y As Integer, i As Integer

For i = 1 To 6
Randomize
loto(i) = CInt(Rnd() * 43) + 6
Call sort

Picture1.Print loto(i);
Next i




End Sub


Private Sub sort()
Dim j As Integer, temp As Integer, i As Integer

For i = 1 To 5
For j = 1 To 5
If loto(j) > loto(j + 1) Then
temp = loto(j)
loto(j) = loto(j + 1)
loto(j + 1) = temp
End If
Next j
Next i
End Sub



I did the ascending guys but there's another issue. It gives duplicate numbers such as 6 12 23 42 42 42:confused:

webbone
12-25-2008, 04:34 PM
You'll need to prevent generation of duplicate numbers in that case :D

There are a couple methods but probably the easiest is to use an array to keep track of which numbers have already been used... check the array each time you generate a new number and if it's used then re-generate the number.

Something like this (in pseudo-code):

Clear Array

Get a number
mark number location in array as used

While [you need more numbers]
Get a number
if not in array then
mark number location in array
decrement count of numbers you need
endif
Loop

Tom Moran
12-25-2008, 06:55 PM
Why are you adding 6 to the picked number? Numbers 1-5 will never get picked.

Also, the best way to randomize a set of numbers is to use a shuffle routine. Assign all possible numbers in an array and then shuffle the array.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum