|
-
Oct 30th, 2009, 04:28 PM
#1
Thread Starter
Junior Member
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)
-
Oct 30th, 2009, 05:08 PM
#2
Re: Problem with non-repeating random numbers
try this:
vb Code:
Dim randomNumber As New Random
Dim usedNumbers As New List(Of Integer)
For x As Integer = 1 To 52
Dim number As Integer = randomNumber.Next(1, 53)
If Not usedNumbers.Contains(number) Then
usedNumbers.Add(number)
Me.lstNumbers.Items.Add(number)
Else
x -= 1
End If
Next
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 30th, 2009, 06:21 PM
#3
Thread Starter
Junior Member
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.
-
Oct 30th, 2009, 06:25 PM
#4
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.
-
Oct 30th, 2009, 06:34 PM
#5
Thread Starter
Junior Member
Re: Problem with non-repeating random numbers
ty, but I want the user to click a button for the next hand.
-
Oct 30th, 2009, 06:48 PM
#6
Re: Problem with non-repeating random numbers
 Originally Posted by ekeak
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.
-
Oct 30th, 2009, 07:02 PM
#7
Re: Problem with non-repeating random numbers
to select a random listbox entry:
vb Code:
Me.lstNumbers.selectedindex = random.Next(1, 53)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 30th, 2009, 08:06 PM
#8
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
-
Nov 12th, 2009, 05:22 PM
#9
Junior Member
Re: Problem with non-repeating random numbers
Last edited by Mido; Nov 12th, 2009 at 07:09 PM.
-
Nov 12th, 2009, 06:32 PM
#10
Re: Problem with non-repeating random numbers
If anyone saw my previous, now deleted, post in this thread it was just a mistake on my part.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|