Random Number Gen with no duplicates - vb.net

Hi Experts,

I'm trying to build a simple number generator (winform) app VB-Express 2010.

** Label1  Label2  Label3  Label4  Label5  Label6  **
**                                                                                        **
**                            BUTTON1                                        **

Click Button 1, all labels will generate numbers between 1, 6
 - thias works great.

But, I want to prevent duplicate PLEASE!

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Dim objRandom As New System.Random( _
  CType(System.DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer))

    Public Function GetRandomNumber( _
      Optional ByVal Low As Integer = 1, _
      Optional ByVal High As Integer = 100) As Integer
        ' Returns a random number,
        ' between the optional Low and High parameters
        Return objRandom.Next(Low, High + 1)
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim intDiceRoll1 As Integer
        Dim intDiceRoll2 As Integer
        Dim intDiceRoll3 As Integer
        Dim intDiceRoll4 As Integer
        Dim intDiceRoll5 As Integer
        Dim intDiceRoll6 As Integer

        intDiceRoll1 = GetRandomNumber(1, 6)
        intDiceRoll2 = GetRandomNumber(1, 6)
        intDiceRoll3 = GetRandomNumber(1, 6)
        intDiceRoll4 = GetRandomNumber(1, 6)
        intDiceRoll5 = GetRandomNumber(1, 6)
        intDiceRoll6 = GetRandomNumber(1, 6)

        Label1.Text = (intDiceRoll1.ToString)
        Label2.Text = (intDiceRoll2.ToString)
        Label3.Text = (intDiceRoll3.ToString)
        Label4.Text = (intDiceRoll4.ToString)
        Label5.Text = (intDiceRoll5.ToString)
        Label6.Text = (intDiceRoll6.ToString)
    End Sub

Open in new window

Roberto (novice)
Who is Participating?
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
CodeCruiserConnect With a Mentor Commented:
Add each number to a list and pass that list along to GetRandomNumber which would check if the number exists in list and then generate it again.
Arthur_WoodConnect With a Mentor Commented:
what you really want is called a Shuffle (like shuufling a deck of cards).

I found this VB.Net code that will do that for you.

 Private Sub shuffle(ByRef arrayToBeShuffled As Array, ByVal numberOfTimesToShuffle As Integer)

     Dim rndPosition As New Random(DateTime.Now.Millisecond)

     For i As Integer = 1 To numberOfTimesToShuffle

          For i2 As Integer = 1 To arrayToBeShuffled.Length

               swap(arrayToBeShuffled(rndPosition.Next(0, arrayToBeShuffled.Length)), arrayToBeShuffled(rndPosition.Next(0, arrayToBeShuffled.Length)))

          Next i2

     Next i

End Sub
 Private Sub swap(ByRef arg1 As Object, ByRef arg2 As Object)

     Dim strTemp As String

     strTemp = arg1

     arg1 = arg2

     arg2 = strTemp

End Sub

you start with an array of size 6, with values 1 through 6.  The Shuffle procedure will return an array of size 6 with the values (1 through 6) randomized.

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

RobertoFreemanoAuthor Commented:
Hi CC,

How do I do this pls?

RobertoFreemanoAuthor Commented:
Hi AW, I'll try your code - thanks ;)
You should probably set the numberOfTimesToShuffle variable to a value like 10.  This is like the number of times you would shuffle a real deck of cards.  When you really shuffle a deck of cards, you NEVER simply shuffle 1 or 2 times.

RobertoFreemanoAuthor Commented:
Thanks Guys ;)
Idle_Mind's solution just worked...
AW = wasn't sure how to connect this up to textbox
CC = Thanks for input

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.