Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › Array of random numbers without duplicates
New Posts  All Forums:Forum Nav:

Array of random numbers without duplicates

post #1 of 4
Thread Starter 
Hi guys,

I'm a little new to coding in VB, so keep that in mind. For a school task I've got to generate an array of 6 random numbers between 1 and 30, making sure there are no duplicates. I have made an attempt which can generate the numbers but I get duplicates. Here is my code:

Dim LottoNums(0 To 5) As String
Dim Flag As Boolean
Dim j, i, Num1, Num2, Num3, Num4, Num5, Sup As Integer
Dim RandomClass As New Random()
Dim RandomNumber As Integer
RandomNumber = RandomClass.Next(1, 31)
Do
Flag = False
For i = 0 To 5
LottoNums(i) = RandomClass.Next(1, 31)
Next
For j = 1 To 5
If LottoNums(0) = LottoNums(j) Then
Flag = True
End If
Next
Loop While Flag = True

Any idea why it doesn't work? I'm stumped; to me that seems completely logical and I can't see why it won't work. If anyone can help I would greatly appreciate it.
post #2 of 4
you're only comparing to see if the first number is duplicated, not if there are any other pairs of numbers generated.
post #3 of 4
Thread Starter 
Hahahaha, I have nothing to say apart from DERP, I guess my brain isn't functioning properly as of late. So do you have a suggestion on how to compare all of them?
post #4 of 4
compare each number as you generate them to see if they have already been drawn in the previous (i-1) numbers.
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › Array of random numbers without duplicates