• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2149
  • Last Modified:

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)
3 Solutions
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.
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.

RobertoFreemanoAuthor Commented:
Hi CC,

How do I do this pls?

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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.

Mike TomlinsonMiddle School Assistant TeacherCommented:
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


Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now