# How to shuffle cards in a card game?

I am new to programming, I am trying to develop an app for Android

Can anyone suggest me how to write code for "shuffling the playing cards"?

-
Welcome to GDSE! You may want to tell us what you've tried, explain a bit more about what you're trying to do (shuffle cards like a visual effect? or in memory?) Also, I doubt you're writing in C and Java, can you narrow that down to a single choice? – Byte56 Mar 31 '12 at 23:46

Card shuffling is an algorithm which is easy to write intuitively, and get entirely wrong by doing so. There's a good reference for implementing card shuffling correctly on Wikipedia. What I'm presenting here is a very slightly simplified version of the algorithm covered on that page under The modern algorithm.

Here's the basic idea, in plain english:

Consider a deck of cards. For this discussion, you can have any number of cards in the deck, and they may start in any order.

We're going to be talking about "position" in the deck, where "position" is how many cards are higher in the deck than the card in that position. For example, the card on top of the deck is at position 0, the card beneath that is at position 1 (because there is 1 card higher than it -- the top card), and in a standard 52-card deck, the bottom card is at position 51, as 51 cards are higher than it in the deck.

Now, we consider each position in the deck, one at a time, starting from the bottom, and working our way up to the top.

For each position, we randomly select one of the cards which is at that position or at a lower-numbered position (remember, the top of the deck is 0, and we're working our way up from the bottom of the deck, so for each position, you're effectively picking up all the cards at and above that position and randomly picking one of those cards).

When we have made the random selection, we swap the card at the position we're currently considering with the card we randomly selected. If we randomly selected the card which was already in that position, then no swap is performed.

After swapping (or not swapping, if we randomly selected the card which was already in the position we were considering), we move on to the next position in the deck and continue.

In pseudocode, with n being the number of cards in the deck, and a being an array representing the deck, the algorithm looks like this:

``````for each i in [n .. 1] do
j ← random integer in [ 0 .. i ]
exchange a[j] and a[i]
``````
-
+1 for more explained and broad answer – Gustavo Maciel Apr 1 '12 at 5:23

You first define a sequence of all the cards you want to shuffle:

``````List<Card> shuffled = new ArrayList<Card>();
``````

Then you walk through every position in the sequence and assign it a card randomly.

``````Random random = new Random();
for (int i = shuffled.size() - 1; i >= 0; i--) {
int j = random.nextInt(i + 1);

/* swap cards i,j */
Card card = shuffled.get(i);
shuffled.set(i, shuffled.get(j));
shufflet.set(j, card);
}
``````

Now `shuffled` is a random sequence of all of your cards.

-
this is known as Knuth's shuffle: en.wikipedia.org/wiki/Knuth_shuffle – krolth Apr 1 '12 at 1:56