Riffing on Riffling

A "riffle shuffle" is the classic way to randomize the order of a deck of cards. You split the deck in half and then "riffle" the cards to interleave the two halves. Astonishingly, some people can perform a "perfect" shuffle, where the riffling precisely alternates between cards from one half of the deck and the other. A weird fact is that doing eight perfect shuffles in a row returns the deck to its original order.

I'd heard of this fact, but wanted to see it for myself. Since I can't do a perfect shuffle I made this visualization. Take a look, then read more below about how this all works.

What you're seeing: Each column shows the order of cards after successive perfect riffle shuffles, with the leftmost column showing the initial order. The cards can be colored in different ways to highlight patterns: by their original position (using colors or grays), by whether they started in the top or bottom half, by whether they were in even or odd positions, or by highlighting the first and last cards. Below each column is a pie chart view of the same order. Below each pie is a scatter plot that shows how cards move: the horizontal position shows a card's original position, while the vertical position shows where it ended up after shuffling.

Why this happens: A perfect riffle shuffle turns out to have an especially simple mathematical description. To begin with, notice the bottom card always stays on the bottom, so we can ignore it in our analysis. For the other 51 cards, the shuffles takes a beautifully simple form. Thinking of the card position as points on a circle, the riffle stretches the circle and wraps it around twice. That is, numbering positions from 0 to 50, a riffle takes a card at position x to the position 2x (mod 51). Touch the diagrams below to see this at work.

Original Order

After Multiplication by 2 (mod 51)

After n shuffles, the card starting at position x will move to position 2nx (mod 51). And by a delicate coincidence, 28 = 1 (mod 51). That means n shuffles will take you back to the original order.

The fact that it only takes eight shuffles really is lucky. If you added two jokers to the deck, or started your interleaving from the bottom half of the deck rather than the top, it would take 52 shuffles to return to the original order. And if you picked a random permutation as your shuffle, it could take thousands. Exploring and generalizing the effects of riffle variations leads to beautiful mathematics: from combinatorics to group theory to famously unanswered questions.

Want to learn more? The internet has plenty of material—perfect shuffles are a beloved topic.