PDA

Click to See Complete Forum and Search --> : Non-repeating Random No's


Gary Collantine
Jan 25th, 2000, 10:40 PM
Apart from being a complete beginner with VB, I would like to know how to generate a group of 6 random numbers (displayed in 6 labels) with none of those numbers being the same.

I am already aware of the Int(RND * 49)+1 code line that creates a random number between 1 and 49. How can i get non-repeating numbers in my question above?

Jan 26th, 2000, 11:07 AM
You could try the following:

Dim x As Integer
Dim y As Integer
Dim Numbers(1 To 50) As Boolean
Dim Labels(1 To 6) As Integer
Dim TempInt As Integer
For x = 1 To 50
Numbers(x) = False
Next x
x = 0
Do
x = x + 1
TempInt = Int((Rnd * 49) + 1)
For y = 1 To 50
If TempInt = y Then
If Not Numbers(y) Then
Numbers(y) = True
Labels(x) = TempInt
Exit For
Else
x = x - 1
Exit For
End If
End If
Next y
Loop Until x = 6
Label1.Caption = Labels(1)
Label2.Caption = Labels(2)
Label3.Caption = Labels(3)
Label4.Caption = Labels(4)
Label5.Caption = Labels(5)
Label6.Caption = Labels(6)



------------------
Boothman
There is a war out there and it is about who controls the information, it's all about the information.

RenegadeMike
Jan 26th, 2000, 11:07 AM
Try this, but first put the labels into an array:

Randomize Timer
For i = 0 To 5
again:
Label1(i).Caption = Int(Rnd * 6) + 1
For j = 0 To 5
If i = j Then GoTo skip
If Label1(i) = Label1(j) Then GoTo again
skip:
Next j
Next i