Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 244,291 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 964 people online right now. Registration is fast and FREE... Join Now!




Random Numbers no repeats

 
Reply to this topicStart new topic

Random Numbers no repeats

dragonseyeangie
17 Nov, 2008 - 07:06 AM
Post #1

New D.I.C Head
*

Joined: 20 Oct, 2008
Posts: 23


My Contributions
Hello, I have an assignment that randomly generates 6 integers I'm not allowed to have any repeat numbers and am not sure how to code it so that it keeps from generating the same number twice for 6 times

CODE

Public Class Form1

    Private rand As New Random

    Const intMAX_SUBSCRIPT As Integer = 5
    Const intMAX_SUBSCRPT As Integer = 4
    Dim intLottery(intMAX_SUBSCRIPT) As Integer
    Dim intPickSix(intMAX_SUBSCRIPT, intMAX_SUBSCRPT) As Integer




    Private Sub btnLottery_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLottery.Click

        Dim intCount As Integer
        Dim intRandNum As Integer

        For intCount = 0 To intMAX_SUBSCRIPT
            intRandNum = rand.Next(55) + 1
            intLottery(intCount) = intRandNum
        Next intCount




        lblNumOne.Text = intLottery(0)
        lblNumTwo.Text = intLottery(1)
        lblNumThree.Text = intLottery(2)
        lblNumFour.Text = intLottery(3)
        lblNumFive.Text = intLottery(4)
        lblNumSix.Text = intLottery(5)

    End Sub

    Private Sub btnPickSix_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPickSix.Click

        intPickSix(0, 0) = rand.Next(54) + 1
        intPickSix(0, 1) = rand.Next(54) + 1
        intPickSix(0, 2) = rand.Next(54) + 1
        intPickSix(0, 3) = rand.Next(54) + 1
        intPickSix(0, 4) = rand.Next(54) + 1
        intPickSix(0, 5) = rand.Next(54) + 1

        intPickSix(1, 0) = rand.Next(54) + 1
        intPickSix(1, 1) = rand.Next(54) + 1
        intPickSix(1, 2) = rand.Next(54) + 1
        intPickSix(1, 3) = rand.Next(54) + 1
        intPickSix(1, 4) = rand.Next(54) + 1
        intPickSix(1, 5) = rand.Next(54) + 1

        intPickSix(2, 0) = rand.Next(54) + 1
        intPickSix(2, 1) = rand.Next(54) + 1
        intPickSix(2, 2) = rand.Next(54) + 1
        intPickSix(2, 3) = rand.Next(54) + 1
        intPickSix(2, 4) = rand.Next(54) + 1
        intPickSix(2, 5) = rand.Next(54) + 1

        intPickSix(3, 0) = rand.Next(54) + 1
        intPickSix(3, 1) = rand.Next(54) + 1
        intPickSix(3, 2) = rand.Next(54) + 1
        intPickSix(3, 3) = rand.Next(54) + 1
        intPickSix(3, 4) = rand.Next(54) + 1
        intPickSix(3, 5) = rand.Next(54) + 1

        intPickSix(4, 0) = rand.Next(54) + 1
        intPickSix(4, 1) = rand.Next(54) + 1
        intPickSix(4, 2) = rand.Next(54) + 1
        intPickSix(4, 3) = rand.Next(54) + 1
        intPickSix(4, 4) = rand.Next(54) + 1
        intPickSix(4, 5) = rand.Next(54) + 1

        lstOutput.Items.Clear()

        lstOutput.Items.Add(intPickSix(0, 0) & "           " & _
                            intPickSix(0, 1) & "           " & _
                            intPickSix(0, 2) & "           " & _
                            intPickSix(0, 3) & "           " & _
                            intPickSix(0, 4) & "           " & _
                            intPickSix(0, 5))
        lstOutput.Items.Add("")
        lstOutput.Items.Add(intPickSix(1, 0) & "           " & _
                            intPickSix(1, 1) & "           " & _
                            intPickSix(1, 2) & "           " & _
                            intPickSix(1, 3) & "           " & _
                            intPickSix(1, 4) & "           " & _
                            intPickSix(1, 5))
        lstOutput.Items.Add("")
        lstOutput.Items.Add(intPickSix(2, 0) & "           " & _
                            intPickSix(2, 1) & "           " & _
                            intPickSix(2, 2) & "           " & _
                            intPickSix(2, 3) & "           " & _
                            intPickSix(2, 4) & "           " & _
                            intPickSix(2, 5))
        lstOutput.Items.Add("")
        lstOutput.Items.Add(intPickSix(3, 0) & "           " & _
                            intPickSix(3, 1) & "           " & _
                            intPickSix(3, 2) & "           " & _
                            intPickSix(3, 3) & "           " & _
                            intPickSix(3, 4) & "           " & _
                            intPickSix(3, 5))
        lstOutput.Items.Add("")
        lstOutput.Items.Add(intPickSix(4, 0) & "           " & _
                            intPickSix(4, 1) & "           " & _
                            intPickSix(4, 2) & "           " & _
                            intPickSix(4, 3) & "           " & _
                            intPickSix(4, 4) & "           " & _
                            intPickSix(4, 5))
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

        lblNumOne.Text = "0"
        lblNumTwo.Text = "0"
        lblNumThree.Text = "0"
        lblNumFour.Text = "0"
        lblNumFive.Text = "0"
        lblNumSix.Text = "0"

        lstOutput.Items.Clear()

    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click

        Me.Close()

    End Sub

    Private Sub btnHelp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHelp.Click

        MessageBox.Show("Welcome to my simple Lottery Program." & ControlChars.CrLf & _
                        ControlChars.CrLf & "Please click the Help button to get help on the Simple " _
                        & "Lottery Program." & ControlChars.CrLf & ControlChars.CrLf & _
                        " Please click the Lottery button to generate/show six unique numbers " & _
                        "in the yellow boxes." & ControlChars.CrLf & ControlChars.CrLf & _
                        "Please click the Pick Six button to generate/show six unique numbers " & _
                        "in the list box." & ControlChars.CrLf & ControlChars.CrLf & _
                        "Enjoy!", "Lottery Help", MessageBoxButtons.OK, MessageBoxIcon.Information)

    End Sub
End Class



User is offlineProfile CardPM
+Quote Post


modi123_1
RE: Random Numbers No Repeats
17 Nov, 2008 - 07:49 AM
Post #2

Suiter #2
Group Icon

Joined: 12 Jun, 2008
Posts: 1,216



Thanked: 49 times
Dream Kudos: 150
My Contributions
Hmm.. get the number from the random generator. If that number exists in an array of arbitrary length get a new number. If not insert it into the array.
User is offlineProfile CardPM
+Quote Post

magicmonkey
RE: Random Numbers No Repeats
17 Nov, 2008 - 08:40 AM
Post #3

D.I.C Regular
***

Joined: 12 Sep, 2008
Posts: 484



Thanked: 93 times
My Contributions
The quick and dirty method would be to loop until the random number generated is not in the array...

vb

Dim intCount As Integer
Dim intRandNum As Integer

'Warning: If intMAX_SUBSCRIPT >= 55 then the Do Loop will go into an endless loop
If intMAX_SUBSCRIPT >= 55 Then Throw New Exception("Endless loop condition.")

For intCount = 0 To intMAX_SUBSCRIPT
Do
intRandNum = rand.Next(55) + 1
Loop Until Not intLottery.Contains(intRandNum)
intLottery(intCount) = intRandNum
Next intCount


A neat way would be to use a Generic HashSet(Of Integer) to add the numbers until the count = 6. A HashSet does not add duplicate numbers, so it is a clever way of accomplishing the task...

vb

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim DrawNumbers As List(Of Integer)
DrawNumbers = RunDraw(6, 1, 55)
End Sub

Private Function RunDraw(ByVal DrawCount As Integer, ByVal DrawMin As Integer, ByVal DrawMax As Integer) As List(Of Integer)
If DrawCount > DrawMax - DrawMin Then
Throw New Exception("Draw count exceeds draw range.")
End If

Dim DrawNumbers As New Generic.HashSet(Of Integer)
Dim RNG As New Random

Do
DrawNumbers.Add(RNG.Next(DrawMin, DrawMax + 1))
Loop Until DrawNumbers.Count = DrawCount

Return DrawNumbers.ToList
End Function

User is offlineProfile CardPM
+Quote Post

dbasnett
RE: Random Numbers No Repeats
17 Nov, 2008 - 08:47 AM
Post #4

D.I.C Head
**

Joined: 1 Oct, 2008
Posts: 224



Thanked: 13 times
My Contributions
CODE
        Dim aRandom As New Random 'random number generator
        Dim aNumber As Integer 'a palce to store a random number
        Dim aRndL As List(Of String)
        aRndL = New List(Of String)
        Do While aRndL.Count < 6
            aNumber = aRandom.Next(1, 57) 'pick a number from 1 - 56 inclusive
            If aRndL.IndexOf(aNumber.ToString) = -1 Then 'not picked
                aRndL.Add(aNumber.ToString) 'add it
            End If
        Loop


This post has been edited by dbasnett: 17 Nov, 2008 - 08:49 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 03:50PM

Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month