0

I have a stack of poker cards (List<Card>) where I want to randomly pick cards when players click on the stack.

To create a 'random card' experience I think about two ways:

  • shuffle cards' positions by reordering the List during initialization, then just pick the first card at static index [0].
  • keep the stack as is and use cards[Random.Range(0,cards.Count)] for selecting a card

(Picking a card also removes it from the list in both cases.)

Are there any relevant differences (statistically or else), dis/advantages, etc. between these two approaches or are they essentially the same?

rene_buehling
  • 728
  • 8
  • 18
  • See my answer from a few minutes ago : https://stackoverflow.com/questions/55665619/generating-random-unique-items-from-a-list/55665734#55665734 Second method you have to test to make sure you do not pick the same card twice or remove card from list. – jdweng Apr 13 '19 at 13:54
  • @jdweng But he says (Picking a card also removes it from the list in both cases). So there is no need for reshuffling, second method will be enough for this kind of action. – mchts Apr 13 '19 at 14:01
  • 1
    The two options you outline are actually one and the same thing. Picking a card with uniform prob and removing it, until there are none left, is a uniform shuffle. – David Heffernan Apr 13 '19 at 16:07

2 Answers2

0

There are two methods here: Random Pick, and Shuffle.

Random Pick involves taking an ordered deck, picking a random card, and drawing that card from the deck.

Shuffle involves taking an ordered deck, picking a random card, and placing that card on top of the new deck, from which you will then draw cards directly.

In both cases, you're going to need to do at most N-1 random number calls, where N is the number of cards in the deck (when there's only one card, you don't need to call the RNG). However in the Random Pick case, you don't have to do those random calls all at once, and not only that, you might not even have to call the Random function N-1 times. If you only end up drawing the top 10 cards, that's only 10 random calls. Also, if you ever need to reshuffle the deck, you can just add the cards back to the deck and then go back to picking at random.

From an algorithmic efficiency perspective, Random Pick is the clear superior choice.

Shuffle the deck anyway.

Here's the thing: You're not trying to pick a random item from a sequence, you're trying to shuffle a deck of cards. Work with the abstraction that matches your design, and deviate from that only when you need to. Hell, I'd almost recommend using a Stack instead of a List simply because it best matches the basic structure of a deck of cards slightly better. But a list might serve you better if you plan to manipulate the deck a lot.

Also, consider this: Random Pick assumes your entire deck is shuffled. You may not always want your entire deck to be shuffled. You may want to explicitly place cards on the top or bottom or in the middle of your deck. It's going to be much harder to do these things if you're trying to get clever with algorithms.

VPellen
  • 1,071
  • 5
  • 10
-1

Kindly have this code to pick up a new random value for an array item random value.

        private void BtnRandomIndex_Click(object sender, EventArgs e)
    {
        int[] Arr = new int[5];

        for (int i = 0; i <= 4; i++)
            Arr[i] = i * 10 ;

        Random Rnd=new Random();
        MessageBox.Show(Rnd.Next((Arr.Length)).ToString());
    }

I hope this can help ^_^

MAMPRO
  • 69
  • 6