Card Shuffling

In this series of interactives, we will explore various mathematical topics related to shuffling decks of cards. In the process, you will hopefully be convinced that riffle shuffling is actually a fairly good technique for shuffling cards.

Perfect Riffles

A riffle shuffle is performed by taking a deck of cards, dividing the deck in half ("cutting" the deck), and then interleaving the two halves. There are two possible ways of interleaving the two halves, one (an in-shuffle) where the top and bottom cards wind up inside the deck, and the other (an out-shuffle) where the top and bottom cards wind up in the same location.

The interactive below will allow you to visualize perfect riffle shuffles:


Number of Cards:
Shuffle Type:
Color Palette:
The first couple of shuffles seem to mix up the deck fairly well, but what happens after many shuffles? Keep playing with the interactive until you're convinced that perfect riffle shuffles aren't a great way of shuffling cards.

Fixed Shuffles

One might wonder if the problem here is caused by the fact that we're shuffling the cards the same way each time, or have we just chosen a particularly bad choice of fixed shuffle? To answer this question, we need some way to talk about deterministic ways of reordering decks of cards. We'll have two ways of presenting a reordering. First, we can present a way of reordering the card as a table. Each column will have a card's original position in the deck above its new position in the deck.

For instance, if we had a three card deck and moved the top card to the bottom, we would represent this as:

123
312

Because the first (top) card moves into the third (bottom) position, the second card moves into the first (top) position, and the third card moves into the second position.

We can also represent these permutations using function diagrams. In these, we draw two decks of cards side by side and draw a line from a card's original position in the first deck to its new position in the second deck. Going back to our three card deck where we move the top card to the bottom, we would represent this as:

Below, you can experiment with various presentations for riffle shuffles:

Number of Cards:
Shuffle Type:
Color Palette:
We can also represent fixed shuffles using a transition diagram. In these, we draw an arrow from a to b if the card in position a winds up in position b.
with cards.

Mouse click to:

Color Palette:

Animate

Usage:

A Little Bit of Randomness

It should hopefully be clear at this point that just applying the same nonrandom shuffle each time to our deck of cards is a bad idea. There are 80,658,175,170,943,878,571,660,636,856,403,766,975,289,505,440,883,277,824,000,000,000,000 different possible orders of cards in a 52 card deck, and we really don't want to just be cycling through a small handful of them.

So let's introduce a little bit of randomness to the deck, as follows:

  1. Randomly pick out some (disjoint) adjacent pairs of cards,
  2. For each pair, switch the cards in the pair.

Specifically, the top and bottom cards have a one in three chance of swapping with the next card in, and the cards in the middle of the deck have a one in four chance of moving up one space, one in two chance of staying put, and a one in four chance of moving down one space. Each card has a chance of moving, and if it does move, it will only move up or down one space in the deck. You also shouldn't worry about the mechanics of how we're going to implement this randomness in a real physical card deck -- I'll explain that later.

Number of Cards: Color Palette: Animate

As before, this isn't a really great way of randomizing the cards in the deck. In this case, cards at the beginning of the deck stay in the beginning of the deck, and cards at the end of the deck stay in the end of the deck. We need to introduce randomness a fair number of times before these trends reduce in intensity, and even then their effects linger for a very long time (it may help to look at the "suits" color palette).

Let's look at this type of shuffling in another way. The interactive below will allow you to visualize the probability that a card starting in a certain position will wind up in a particular position after a certain number of shuffles. In the first column, only one cell is filled in, representing the position the card begins in. In the second column, we see the possible positions the card could wind up in after our first introduction of randomness, labeled by the probability of our card winding up in those positions. In the third column, we see the possible positions the card could wind up in after introducing randomness twice, labeled by the probability of our card winding up in those positions.

Hint: Hover your mouse over a box to get more information about what it's telling you.

Number of cards: Number of shuffles: Starting Position: Show Probability as:

Notice that even for a fairly small sized deck (12 cards) and a lot of shuffles (30 shuffles), our initial card is significantly more likely to wind up in some positions than others. This indicates that the deck is not nearly randomly shuffled. Play around with the interactive to get a sense of just how many shuffles you need before the card is roughly equally likely to wind up in any position in the deck.

A Normal Side Note

An interesting pattern emerges if we start with a card in the middle of a really large deck of cards and plot the probabilities of that card winding up in various positions, using a properly scaled bar chart.

Number of Shuffles: Show Gaussian
As the number of shuffles increases, the tops of the bars in the bar graph above get closer and closer to forming a particular curve. This curve is called a Gaussian or Normal curve, and is given by a suitable transformation of the equation y=e-x².

This is neat, but, alas, this is not what we're looking for. We're hoping that the distribution of probabilities is flat, that is, the probabilities are roughly equal that a card will wind up in any position.

Combining Shuffles

Finally, let's combine our two shuffling techniques to produce an imperfect riffle.

This is a mathematical model of real world riffle shuffling: cards wind up roughly where they would with a perfect riffle shuffle, but may be moved slightly.

The interactive below will allow you to visualize the probability of a card starting in a particular position winding up in a particular position after some number of shuffles of this type.

Number of cards: Number of shuffles: Starting Position: Show Probability as:
How many shuffles does it take to even out the probabilities for 12 cards? 52 cards? 100 cards? Hint: use the horizontal bar graph (scaled) visualization.

It's clear that our imperfect riffle is much better than just doing our introduce a small amount of randomness operation. But why?

Chaos and Scale

A system is said to be chaotic if small changes in its input or initial configuration lead to large changes in its output or final configuration.

Note that this definition agrees in part with our intuitive use of the word chaotic in the sense of being unpredictable. Our ability to measure real-world systems is limited: small changes to a system might be too small for us to measure, but, if changes are magnified by the system, those immeasurably small initial changes may produce fairly substantial changes to the system that we weren't able to predict.

However our mathematical definition of chaotic doesn't exclude the possibility of the system being entirely unrandom in its behavior.

A good source of chaos is iterative systems, systems where the same operation is applied repeatedly, such as our card shuffling operation. Although our perfect riffle shuffle is entirely predictable in its behavior, it nonetheless meets our definition of chaotic. If two cards start out next to each other or near each other, their distance gets repeatedly doubled until they wind up in separate halves of the cut. (You can visualize this using the "First Two" or "Random Two" color palettes in the interactive at the top of the page.) We can alternately phrase this as: if we change the starting position of a card, the distance between where the card winds up and where it would have wound up gets repeatedly doubled (until they wind up in separate halves of the cut). While the first few doublings don't produce a significant effect, the effect becomes very significant as the number of doublings increases.

So what happens when we alternate our perfect riffle shuffle with our small introduction of randomness?

We can follow these changes in the table below:

TimeEffect of 1st randomnessEffect of 2nd randomnessEffect of 3rd randomnessEffect of 4th randomnessEffect of 5th randomnessEffect of 6th randomnessEffect of 7th randomness
1st perfect riffle
1st randomness1 space
2nd perfect riffle2 spaces
2nd randomness2 spaces1 space
3rd perfect riffle4 spaces2 spaces
3rd randomness4 spaces2 spaces1 space
4th perfect riffle8 spaces4 spaces2 spaces
4th randomness8 spaces4 spaces2 spaces1 space
5th perfect riffle16 spaces8 spaces4 spaces2 spaces
5th randomness16 spaces8 spaces4 spaces2 spaces1 space
6th perfect riffle32 spaces16 spaces8 spaces4 spaces2 spaces
6th randomness32 spaces16 spaces8 spaces4 spaces2 spaces1 space
7th perfect riffle64 spaces32 spaces16 spaces8 spaces4 spaces2 spaces
7th randomness64 spaces32 spaces16 spaces8 spaces4 spaces2 spaces1 space

Since these changes are all at different scales, they don't interfere with each other much, and the changes quickly get large enough to cover the entire deck.

Compare this with just applying the small introduction of randomness operation over and over again: Each operation introduces a change at the scale of one space, and so risk cancelling each other out.