Results 1 to 10 of 10

Thread: Problem with non-repeating random numbers

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2007
    Posts
    26

    Question Problem with non-repeating random numbers

    Why is my application messing up? I'm trying to make non-repeating random numbers and then insert them into a listbox. Check my code out:
    Code:
    Dim RandomNumber As String
    Dim x As Integer
    
            Do
    Start:
                Randomize()
    
                RandomNumber = Int((52 * Rnd()) + 1) ' Random number 1 to 52
                Me.lstNumbers.Items.Add(RandomNumber)
            Loop Until RandomNumber <> Me.lstNumbers.Items.Item(x)

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Problem with non-repeating random numbers

    try this:

    vb Code:
    1. Dim randomNumber As New Random
    2. Dim usedNumbers As New List(Of Integer)
    3. For x As Integer = 1 To 52
    4.     Dim number As Integer = randomNumber.Next(1, 53)
    5.     If Not usedNumbers.Contains(number) Then
    6.         usedNumbers.Add(number)
    7.         Me.lstNumbers.Items.Add(number)
    8.     Else
    9.         x -= 1
    10.     End If
    11. Next

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2007
    Posts
    26

    Re: Problem with non-repeating random numbers

    Code:
            Dim RandomNumber As Integer
            Dim Random As New Random
            Dim usedNumbers As New List(Of Integer)
            Dim a As Integer
    
            For x As Integer = 1 To 52
    
                Dim number As Integer = random.Next(1, 53)
    
                If Not usedNumbers.Contains(number) Then
    
                    usedNumbers.Add(number)
    
                    Me.lstNumbers.Items.Add(number)
    
                Else
    
                    x -= 1
    
                End If
            Next
    
            RandomNumber = Me.lstNumbers.Items.Item(a)
    How do i select the next item in the list. For a card deck, it always selects the first. Thanks.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Problem with non-repeating random numbers

    Follow the CodeBank link in my signature. There are two threads on random selections from lists. One makes random selections for the list and then removes that item, so it cannot be selected again, while the other randomises the list in-place, so you can just loop through it from start to finish.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Nov 2007
    Posts
    26

    Thumbs up Re: Problem with non-repeating random numbers

    ty, but I want the user to click a button for the next hand.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Problem with non-repeating random numbers

    Quote Originally Posted by ekeak View Post
    ty, but I want the user to click a button for the next hand.
    No problem. Use the first option. Create a list and assign it to a member, i.e. class-level, variable. Each time the user clicks the button you get an item randomly from the list and remove it so that it can't be selected again. Just make sure that your Random object is assigned to a member variable too, so you use the same one all the time and don't create a new one each time you want to make a selection.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Problem with non-repeating random numbers

    to select a random listbox entry:

    vb Code:
    1. Me.lstNumbers.selectedindex = random.Next(1, 53)

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Problem with non-repeating random numbers

    Try this. Create a new project and add a Button to the form. Now replace the form code with this:
    Code:
    Public Class Form1
    
        Private Enum CardSuit
            Hearts
            Clubs
            Diamonds
            Spades
        End Enum
    
        Private Enum CardFace
            Ace
            Two
            Three
            Four
            Five
            Six
            Seven
            Eight
            Nine
            Ten
            Jack
            Queen
            King
        End Enum
    
        Private Structure Card
            Public Face As CardFace
            Public Suit As CardSuit
    
            Public Overrides Function ToString() As String
                Return String.Format("{0} of {1}", Me.Face, Me.Suit)
            End Function
        End Structure
    
        Private deck As New List(Of Card)
        Private cardPicker As New Random
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.LoadDeck()
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If Me.deck.Count = 0 Then
                If MessageBox.Show("Would you like to reload the deck?", _
                                   "Deck Empty", _
                                   MessageBoxButtons.YesNo, _
                                   MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                    Me.LoadDeck()
                End If
            End If
    
            If Me.deck.Count > 0 Then
                Dim index As Integer = Me.cardPicker.Next(0, Me.deck.Count)
                Dim card As Card = Me.deck(index)
    
                Me.deck.RemoveAt(index)
    
                MessageBox.Show(card.ToString(), "Your Card")
            End If
        End Sub
    
        Private Sub LoadDeck()
            Dim card As Card
    
            For Each suit As CardSuit In [Enum].GetValues(GetType(CardSuit))
                For Each face As CardFace In [Enum].GetValues(GetType(CardFace))
                    card = New Card
                    card.Face = face
                    card.Suit = suit
                    Me.deck.Add(card)
                Next
            Next
        End Sub
    
    End Class
    Run that a click the button as many times as you like. That uses the principle in the first thread of mine I mentioned. Now here's some similar code that uses the second:
    Code:
    Public Class Form1
    
        Private Enum CardSuit
            Hearts
            Clubs
            Diamonds
            Spades
        End Enum
    
        Private Enum CardFace
            Ace
            Two
            Three
            Four
            Five
            Six
            Seven
            Eight
            Nine
            Ten
            Jack
            Queen
            King
        End Enum
    
        Private Structure Card
            Public Face As CardFace
            Public Suit As CardSuit
    
            Public Overrides Function ToString() As String
                Return String.Format("{0} of {1}", Me.Face, Me.Suit)
            End Function
        End Structure
    
        Private deck As New List(Of Card)
        Private cardPicker As New Random
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.LoadDeck()
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If Me.deck.Count = 0 Then
                If MessageBox.Show("Would you like to reload the deck?", _
                                   "Deck Empty", _
                                   MessageBoxButtons.YesNo, _
                                   MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                    Me.LoadDeck()
                End If
            End If
    
            If Me.deck.Count > 0 Then
                Dim card As Card = Me.deck(0)
    
                Me.deck.RemoveAt(0)
    
                MessageBox.Show(card.ToString(), "Your Card")
            End If
        End Sub
    
        Private Sub LoadDeck()
            Dim card As Card
    
            For Each suit As CardSuit In [Enum].GetValues(GetType(CardSuit))
                For Each face As CardFace In [Enum].GetValues(GetType(CardFace))
                    card = New Card
                    card.Face = face
                    card.Suit = suit
                    Me.deck.Add(card)
                Next
            Next
    
            Me.deck = Me.deck.OrderBy(Function() Me.cardPicker.NextDouble()).ToList()
        End Sub
    
    End Class
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    Junior Member
    Join Date
    Nov 2009
    Posts
    17

    Re: Problem with non-repeating random numbers

    Please Help me here in VB6 Code, Thanks.
    http://www.vbforums.com/showthread.php?t=591635
    Last edited by Mido; Nov 12th, 2009 at 07:09 PM.

  10. #10

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width