1

I have a button on my windows form which add five random cards to my list. The random cards are displayed in a listbox. When I first press the button, the listbox show 5 random cards. When I press the button again, the listbox shows the same 5 random cards 3 times now. Instead of 10 random cards. Is there anyway to get rid of this problem?

 private void RandomButton_Click(object sender, EventArgs e)
    {
        Random random = new Random();
        for (int i = 0; i < 5; i = i + 1)
            fiveRandomCards.Add(new Card((Symbool)random.Next(1, 4), (Waarde)random.Next(1, 14)));
        foreach (Card card in fiveRandomCards)
            CardsLB.Items.Add(card.name);                
    }
Steven
  • 1,996
  • 3
  • 22
  • 33
P.Sienaert
  • 11
  • 3
  • 2
    Don't create a new `Random` object every time you click the button. Create a class field for it and instantiate it when the form is created. – itsme86 Oct 10 '17 at 14:46
  • 1
    Off topic: please try to use English instead of your native language when naming variables, methods etc. You and other developers will find it very useful, believe me – Alex Oct 10 '17 at 14:49
  • start with a full deck of cards, remove 5 and place them in the list - now you cant reselect them – BugFinder Oct 10 '17 at 14:50
  • 1
    You are not removing items from list. Note that to actually write what you described you need to shuffle full list of cards and take first five. Search for "c# randomize list". – Alexei Levenkov Oct 10 '17 at 14:58

2 Answers2

0

I actually like this approach for small collections

linq: order by random

pseudo code

ICollection<int> coll = new List<int>() {1,2,3,4,5,6,7,8,9,10,11,12,13,14};
ICollection<int> randomFiveItems = coll.OrderBy(x => Guid.NewGuid()).Take(5);

Then bind randomFiveItems that do your control

KaartenLB.DataSource = randomFiveItems;

Klar für mich

granadaCoder
  • 26,328
  • 10
  • 113
  • 146
0

I found a solution for my problem, it is quite simple. I have 2 lists in my program: The first list is "deck", which already holds 52 cards, those are generated in my Form1_load method. The second list is "fiveRandomCards". On my windows form i have a button. When I click on this button it adds 5 radom cards from my deck list to the fiveRandomCards list. I added a clear() method to my listbox so the items in the listbox won't stack.

Keep in mind, in my Card class, I uses 2 enums: Symbol -> diamonds, clubs, hearts and spades. Value -> from 1 to 13.

public partial class Form1 : Form
{
    List<Card> deck = new List<Card>(); //allready has 52 cards in it.
    List<Card> fiveRandomCards = new List<Card>(); 
    Random random = new Random();

    private void Form1_Load(object sender, EventArgs e)
        {
            for (int symbol = 1; symbol < 5; symbol = symbol + 1)
            {
                for (int value = 1; value < 14; value = value + 1)
                    deck.Add(new Card((Symbol)symbol, (Value)value));
            }
        }
    private void RandomButton_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < 5; i = i + 1)
            fiveRandomCards.Add(deck[random.Next(0, deck.Count)]); 
        CardsLB.Items.Clear();
        foreach (Card card in fiveRandomCards)
            CardsLB.Items.Add(card.name);                
    }
}
P.Sienaert
  • 11
  • 3