Random Number Gen with no duplicates -

Posted on 2012-09-21
Last Modified: 2012-09-21
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)
Question by:RobertoFreemano
    LVL 83

    Assisted Solution

    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.
    LVL 44

    Assisted Solution

    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.


    Author Comment

    Hi CC,

    How do I do this pls?


    Author Comment

    Hi AW, I'll try your code - thanks ;)
    LVL 44

    Expert Comment

    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.

    LVL 85

    Accepted Solution


    Author Closing Comment

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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now