# Is there any way to get unique numbers for a label?

• ### Question

• Im writing a lottery code and would like to know if there is anyway to get my 6 numbers in my label to be different

this is my code so far

```Public Class frmMain

Private randGen As New Random

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub

Private Sub btnSelect_Click(sender As Object, e As EventArgs) Handles btnSelect.Click

Dim intNum1 As Integer
Dim intNum2 As Integer
Dim intNum3 As Integer
Dim intNum4 As Integer
Dim intNum5 As Integer
Dim intNum6 As Integer

intNum1 = randGen.Next(1, 54)
intNum2 = randGen.Next(1, 54)
intNum3 = randGen.Next(1, 54)
intNum4 = randGen.Next(1, 54)
intNum5 = randGen.Next(1, 54)
intNum6 = randGen.Next(1, 54)

lblNumbers.Text = String.Format("{0}, {1}, {2}, {3}, {4}, {5}", intNum1, intNum2, intNum3, intNum4, intNum5, intNum6)

End Sub

End Class
```

Thursday, March 19, 2015 5:41 PM

• If you want all 6 numbers to be different, I suggest putting all the possible numbers (1-53 in your example) in a list and picking 6 numbers at random from the list (removing each number from the list when you pick it). You can store the numbers in an array and use String.Join to display them in a label.

```Private randGen As New Random

Sub btn_Select_Click(sender As Object, e As EventArgs) Handles btnSelect.Click
Dim allNumbers As List(Of Integer) = Enumerable.Range(1, 53).ToList 'will be numbers 1-53
Dim intNum(5) As Integer

For i As Integer = 0 To 5
Dim pos As Integer = randGen.Next(0, allNumbers.Count)
intNum(i) = allNumbers(pos)
allNumbers.RemoveAt(pos)
Next

lblNumbers.Text = String.Join(" ", intNum.Select(Function(i) i.ToString))
End Sub```

• Proposed as answer by Thursday, March 19, 2015 5:59 PM
• Edited by Thursday, March 19, 2015 6:12 PM
• Marked as answer by Thursday, March 19, 2015 7:01 PM
Thursday, March 19, 2015 5:56 PM

### All replies

• If you want all 6 numbers to be different, I suggest putting all the possible numbers (1-53 in your example) in a list and picking 6 numbers at random from the list (removing each number from the list when you pick it). You can store the numbers in an array and use String.Join to display them in a label.

```Private randGen As New Random

Sub btn_Select_Click(sender As Object, e As EventArgs) Handles btnSelect.Click
Dim allNumbers As List(Of Integer) = Enumerable.Range(1, 53).ToList 'will be numbers 1-53
Dim intNum(5) As Integer

For i As Integer = 0 To 5
Dim pos As Integer = randGen.Next(0, allNumbers.Count)
intNum(i) = allNumbers(pos)
allNumbers.RemoveAt(pos)
Next

lblNumbers.Text = String.Join(" ", intNum.Select(Function(i) i.ToString))
End Sub```

• Proposed as answer by Thursday, March 19, 2015 5:59 PM
• Edited by Thursday, March 19, 2015 6:12 PM
• Marked as answer by Thursday, March 19, 2015 7:01 PM
Thursday, March 19, 2015 5:56 PM
• Try this too:

```Dim a = Enumerable.Range(1, 53).OrderBy(Function() randGen.Next).Take(6).ToArray

intNum1 = a(0)
intNum2 = a(1)
intNum3 = a(2)
intNum4 = a(3)
intNum5 = a(4)
intNum6 = a(5)
```

• Edited by Thursday, March 19, 2015 6:02 PM
• Proposed as answer by Thursday, March 19, 2015 6:22 PM
Thursday, March 19, 2015 6:02 PM
• Karodhill,

No matter how you go about doing this, these numbers are repeatable. Don't confuse randomness with uniqueness; the two really have nothing to do with each other.

A GUID is a better choice, but it's letters and numbers and it's much longer than six! If you're sure you want six, then at least keep track of the ones which were selected previously and set your program up to continue generating a new one until it's not in that list.

Do also know though that at some point in time, that list will be "full" and you need to calculate the total possible combinations so to know when you're "done". Otherwise you'll end up in an endless loop; you tell it to continue until it's not in the list and ... there are none to be had.

For what it's worth... :)

Still lost in code, just at a little higher level.

:-)

Thursday, March 19, 2015 6:14 PM
• Try this too:

```Dim a = Enumerable.Range(1, 53).OrderBy(Function() randGen.Next).Take(6).ToArray

intNum1 = a(0)
intNum2 = a(1)
intNum3 = a(2)
intNum4 = a(3)
intNum5 = a(4)
intNum6 = a(5)```

Viorel,

How does that prevent each of them from being the same value?

Maybe I'm missing it?

Still lost in code, just at a little higher level.

:-)

Thursday, March 19, 2015 6:15 PM
• @Frank. The code I posted does not repeat any numbers (as I explained in the post); this is a standard way of randomly choosing numbers in the style of a lottery. I don't think Viorel_'s code repeats numbers either.
Thursday, March 19, 2015 6:18 PM
• @Frank. The code I posted does not repeat any numbers (as I explained in the post); this is a standard way of randomly choosing numbers in the style of a lottery. I don't think Viorel_'s code repeats numbers either.

Then please explain to me how his doesn't (I can see that yours won't).

It's "taking" six ... from the random set. What's to prevent one of those from being same value?

I wasn't trying to cast aspersions here! I just don't see how it avoids it.

Still lost in code, just at a little higher level.

:-)

Thursday, March 19, 2015 6:20 PM
• @Frank. The code I posted does not repeat any numbers (as I explained in the post); this is a standard way of randomly choosing numbers in the style of a lottery. I don't think Viorel_'s code repeats numbers either.

Oh I do see!

He's ordering them randomly then taking six - so of course each has to be different!

Very clever Viorel! :)

Still lost in code, just at a little higher level.

:-)

Thursday, March 19, 2015 6:22 PM
• by unique i mean at the instant the button is pressed, on the same label line none of those will be the same
Thursday, March 19, 2015 6:23 PM
• @Frank. The code I posted does not repeat any numbers (as I explained in the post); this is a standard way of randomly choosing numbers in the style of a lottery. I don't think Viorel_'s code repeats numbers either.

Then please explain to me how his doesn't (I can see that yours won't).

It's "taking" six ... from the random set. What's to prevent one of those from being same value?

I wasn't trying to cast aspersions here! I just don't see how it avoids it.

Still lost in code, just at a little higher level.

:-)

No problem Frank.
Thursday, March 19, 2015 6:25 PM
• by unique i mean at the instant the button is pressed, on the same label line none of those will be the same
Then you have two good examples shown here. :)

Still lost in code, just at a little higher level.

:-)

Thursday, March 19, 2015 6:26 PM
• Here's one way using nested For loops.  This was tested and it works.  It will select 6 different unique numbers between 1 - 54.

```    Private Sub btnOK_Click(sender As System.Object, e As System.EventArgs) Handles btnOK.Click
Dim num As Integer, dup As Boolean = False
Dim randnum As New Random()
Dim strand As String = ""
Dim itm As String = ""
For x As Integer = 1 To 6
For y As Integer = 1 To 6
num = randnum.Next(1, 55)
itm = num.ToString()
If strand.Contains(itm) Then
dup = True
x = x - 1
Exit For
End If
Next y
If dup = False Then strand &= itm & "  "
dup = False
Next x
lblRandom.Text = strand
End Sub```

Here is another way using the Shuffle Sort algorithm with an array:

```    Private Sub btnShuffle_Click(sender As System.Object, e As System.EventArgs) Handles btnShuffle.Click
Dim mix, temp As Integer
Dim randnum As New Random
Dim strand(54) As Integer
lblRandom.Text = ""
For x As Integer = 1 To 54
strand(x) = x
Next
For x As Integer = 1 To 54
mix = randnum.Next(1, 55)
temp = strand(mix)
strand(mix) = strand(x)
strand(x) = temp
Next
For x As Integer = 1 To 6
lblRandom.Text &= strand(x).ToString & "  "
Next
End Sub```

Solitaire

• Proposed as answer by Thursday, March 19, 2015 6:59 PM
Thursday, March 19, 2015 6:46 PM