Random Numbers
Hi All
Can anyone tell me how to sort random numbers in ascending order. I am doing a project which requires me to design an application that generates six numbers , each number must be an integer and is chosen from the range 1 to 49.
This is a lottery project, the six numbers selected have to be displayed in ascending order just like the national lottery.
I would be grateful for any help.
[419 byte] By [
Reilly] at [2007-11-17 17:10:13]

# 1 Re: Random Numbers
This little code will generate your 6 random numbers in a Array and will sort them. Just one thing, you may have duplicates.
For iCtr = 1 To 6
aRand(iCtr) = Int(Rnd() * 49) + 1
Next iCtr
For iCtr = 1 To 5
For iCtr2 = iCtr To 6
If (aRand(iCtr) > aRand(iCtr2)) Then
iTmp = aRand(iCtr)
aRand(iCtr) = aRand(iCtr2)
aRand(iCtr2) = iTmp
End If
Next iCtr2
Next iCtr
Heulsay
# 2 Re: Random Numbers
Try this code
------------------
Option Explicit
Private Sub SetRandomNubers()
Const TotalNumbers As Integer = 49
Const TotalGeneratesNumbers As Integer = 6
Dim NumArray() As Integer
Dim Index As Integer
Dim RandomNumber As Integer
Dim strRandomNumber As String
'Set an Array
ReDim NumArray(TotalNumbers)
For Index = 1 To TotalNumbers
NumArray(Index) = 0
Next Index
'Generates the numbers
For Index = 1 To TotalGeneratesNumbers
Do
RandomNumber = Int(Rnd() * TotalNumbers) + 1
Loop Until NumArray(RandomNumber) = 0
NumArray(RandomNumber) = 1
Next Index
'Show the numbers in ascending order
strRandomNumber = ""
For Index = 1 To TotalNumbers
If NumArray(Index) = 1 Then
strRandomNumber = strRandomNumber & Str(Index) & ", "
End If
Next Index
Text1 = strRandomNumber
End Sub
Private Sub Command1_Click()
Call SetRandomNubers
End Sub
Private Sub Form_Load()
Randomize
End Sub
------------------
efi
efi at 2007-11-10 0:23:40 >

# 3 Re: Random Numbers
Dim key As Integer
Randomize
key = Rnd() * 49
# 4 Re: Random Numbers
The super lazy way, would be to set up an array with 49 elements (integer). When you select a number, you set that element to the number eg if you select number 10, you set element number 10 to 10 if you select 30, you set element 30 to 10. After you have selected the numbers, you have selected all six numbers, you execute a loop which goes through the 49 elements and only displays those which do not have a zero value.
The more correct way, would be to have two arrays, one which has your six elements and the second which will contain the sorted numbers. You would have a loop going through the first array looking for the lowest number, and once found moving it to the available element in the sorted array. When you move the number, you set its element to 0 and ignore all values of zero.
eg
dim nonsort(6) as integer
dim sort(6) as integer
dim lownum as integer
dim lowcnt as integer
dim sortcount as integer
dim position as integer
fill nonsort. - your function
lownum = 999
sortcount = 1
for sortcount = 1 to 6
for position = 1 to 6
if nonsort(position) < lownum and nonsort(position) <> 0 then
lownum = nonsort(position)
lowcnt = position
end if
Next Position
sort(sortcount) = lownum
nonsort(lowcnt) = 0
Next SortCount
I haven't tried the above out, but think it should work.
HTH
# 5 Re: Random Numbers
Option Explicit
'Add a Listbox and a commandbutton to a form.
'Set the Listbox Sorted property to True.
Private Sub Command1_Click()
Dim i As Integer, number As Integer
Randomize
List1.Clear
Do While List1.ListCount < 6
number = Int(49 * Rnd + 1)
List1 = number
If List1.ListIndex = -1 Then List1.AddItem Format(number, "00")
Loop
For i = 0 To List1.ListCount - 1
Debug.Print List1.List(i); " ";
Next
Debug.Print
End Sub
MKSa at 2007-11-10 0:26:37 >
