puzzle #16
In this lesson

Wide tables, equations, and code scroll sideways. Swipe, or Tab to focus them and use the left and right arrow keys.

Riffle shuffle

You will learn: Distinguish a shuffled sample from its distribution and quantify convergence to uniform permutations.

Start with: Events and probability

“Seven shuffles randomizes a deck” needs both a shuffle model and a definition of close enough. Under the Gilbert–Shannon–Reeds model, seven shuffles of fifty-two distinct cards leave a total-variation distance of about 0.334 from uniform. Eight reduce it to about 0.167. Seven is a useful landmark in a transition, not a declaration of perfect randomness.

The experiment tracks one repeatable deck while separately calculating the full distribution’s distance from uniform. A deck that looks disorderly may still come from a highly nonuniform shuffle procedure. Conversely, a uniform shuffle can occasionally produce an orderly-looking arrangement.

Cut, interleave, and inspect

Original card labels at their current deck positions10203040501020304050current positionoriginal card label

Card 1 is at position 8, marked by the larger ring. Its positions after successive shuffles are 1, 2, 6, 19, 40, 28, 6, 8. Increasing the shuffle count continues the same history.

Current order: 4, 52, 30, 45, 24, 10, 49, 1, 20, 41, 47, 39, 48, 5, 22, 27, 21, 11, 34, 31, 2, 7, 25, 13, 16, 40, 23, 3, 18, 44, 37, 32, 17, 38, 6, 46, 15, 42, 35, 26, 12, 33, 50, 14, 9, 36, 28, 29, 8, 19, 43, 51. This deck has 23 rising sequences of successive original labels.

Total-variation distance from the uniform permutation distribution00.20.40.60.81024681012riffle shufflestotal-variation distance

Exact-model distance after 7 shuffles: 0.3340610. This describes the distribution over all decks, not a randomness score assigned to this one displayed deck. Values near one retain the small overlap instead of rounding to certainty.

ShufflesTotal-variation distance
01 − 1.239800e-68
11 − 5.583563e-53
21 − 2.514571e-37
31 − 1.076145e-21
41 − 4.665686e-7
50.9237329
60.6135496
70.3340610
80.1671586
90.08542019
100.04294555
110.02150238
120.01075489
Inspect the rising sequences and distribution

Each group consists of consecutive original labels that still occur in increasing position order. A group need not occupy adjacent positions.

  1. 1, 2, 3
  2. 4, 5, 6
  3. 7, 8
  4. 9
  5. 10, 11, 12
  6. 13, 14
  7. 15
  8. 16, 17
  9. 18, 19
  10. 20, 21
  11. 22, 23
  12. 24, 25, 26
  13. 27, 28, 29
  14. 30, 31, 32, 33
  15. 34, 35, 36
  16. 37, 38
  17. 39, 40
  18. 41, 42, 43
  19. 44
  20. 45, 46
  21. 47, 48
  22. 49, 50, 51
  23. 52

Cut sizes in this history: 32, 28, 21, 25, 26, 24, 30

Rising sequences rProbability per arrangementCurrent probability of rUniform probability of r
11.222312e-641.222312e-641.239800e-68
28.672268e-653.905642e-495.583563e-53
36.138796e-653.966326e-408.010449e-44
44.335308e-658.792901e-342.514571e-37
53.054422e-656.778895e-292.751576e-32
62.146822e-656.221591e-253.592999e-28
71.505243e-651.303324e-211.073489e-24
81.052800e-659.129175e-191.075071e-21
97.345117e-662.719537e-164.590373e-19
105.111514e-664.043872e-149.808429e-17
113.547992e-663.355835e-121.172653e-14
122.456302e-661.684830e-108.504050e-13
131.696018e-665.435068e-93.973069e-11
141.167917e-661.179607e-71.252209e-9
158.020633e-670.0000017854562.759892e-8
165.492918e-670.000019391224.376769e-7
173.751261e-670.00015460620.000005109769
182.554540e-670.00092181760.00004473876
191.734564e-670.0041724540.0002982310
201.174332e-670.014513970.001532311
217.926742e-680.039187440.006129200
225.334349e-680.082783710.01924044
233.578740e-680.13769630.04770277
242.393425e-680.18120810.09386622
251.595617e-680.18933430.1471134
261.060313e-680.15741950.1840671
277.022852e-690.10426490.1840671
284.636001e-690.055010320.1471134
293.050000e-690.023091790.09386622
301.999669e-690.0076939640.04770277
311.306450e-690.0020274790.01924044
328.505080e-700.00042046570.006129200
335.516809e-700.000068184130.001532311
343.565285e-700.0000085762090.0002982310
352.295457e-708.283264e-70.00004473876
361.472259e-706.067836e-80.000005109769
379.406098e-713.320561e-94.376769e-7
385.985699e-711.332464e-102.759892e-8
393.793753e-713.831725e-121.252209e-9
402.394638e-717.673870e-143.973069e-11
411.505201e-711.032449e-158.504050e-13
429.421043e-728.910805e-181.172653e-14
435.871085e-724.644791e-209.808429e-17
443.642644e-721.348693e-224.590373e-19
452.249868e-721.950935e-251.075071e-21
461.383252e-721.197698e-281.073489e-24
478.464679e-732.453104e-323.592999e-28
485.155180e-731.144126e-362.751576e-32
493.124352e-736.336831e-422.514571e-37
501.884151e-731.217365e-488.010449e-44
511.130491e-735.091277e-585.583563e-53
526.747890e-746.747890e-741.239800e-68
Gilbert–Shannon–Reeds model: a Binomial(N, 1/2) cut, then drop each next card from a packet with probability proportional to its remaining size. Order inside each packet is preserved. This model does not describe a perfect alternating faro shuffle.

Cards begin with distinct labels 1 through N. For each shuffle, the cut size has a Binomial(N, 1/2) distribution. The two contiguous packets are then interleaved. If L cards remain in the first packet and R in the second, the next output card comes from the first with probability L/(L + R). Order within each packet is preserved. Empty packets are valid boundary outcomes.

This construction differs from always cutting exactly in half and alternating perfectly. That deterministic operation is a faro shuffle and has its own cycle structure. The control here repeats the random cut-and-interleave procedure, and “Riffle once more” extends the same sampled history.

What one shuffle can produce

For three cards, there are eight equally likely cut-and-interleaving patterns. Four produce the identity order 1, 2, 3: either empty packet, and the two nonempty cuts whose interleaving restores the order. Each of the arrangements 1, 3, 2; 2, 1, 3; 2, 3, 1; and 3, 1, 2 occurs once. The reverse order 3, 2, 1 cannot occur after one riffle.

Thus the identity has probability 1/2, four other permutations have probability 1/8, and the reverse has probability zero. Uniform randomness would give every permutation probability 1/6. Half the sum of absolute differences is 1/3. Set N = 3 and one shuffle to inspect this small case in the model table.

Follow successive original labels

A rising sequence is a maximal group of consecutive original labels whose positions still increase. The labels need not be adjacent in the shuffled deck. For example, the arrangement 1, 5, 2, 3, 6, 7, 4 contains the two rising sequences (1, 2, 3, 4) and (5, 6, 7). Counting adjacent increases in the displayed deck would answer a different question.

After m riffles, put a = 2ᵐ. The probability of a particular arrangement with r rising sequences is C(a + N − r, N) / aᴺ. If a < r, the binomial coefficient is zero: that arrangement is unreachable at that shuffle count. There are Eulerian-number counts of arrangements for each possible r. The calculation groups permutations by r instead of enumerating all N! arrangements.

The implementation obtains the integer counts before converting probabilities to floating-point values. Its small-deck results are checked against all permutations through seven cards and direct cut/interleaving histories. These checks establish which direction of the permutation is used when counting rising sequences.

Read the distance correctly

Total variation is the largest probability discrepancy over any event made from deck arrangements. Equivalently, it is half the sum of the absolute differences between the two probability distributions. A distance of 0.334 does not mean that 33.4% of individual cards are “unshuffled.” It describes the shuffle distribution as a whole.

For fifty-two cards, the distances after five, six, seven, and eight riffles are about 0.923733, 0.613550, 0.334061, and 0.167159. The chart plots this sequence regardless of which seed illustrates the sample deck. Changing the seed changes the deck, not these theoretical distances. With one card, every shuffle distribution is already uniform.

Make a prediction

If the displayed deck happens to return to its original order, does that prove the shuffler is nonrandom?

Explore the answer

No. The original order is one possible outcome under uniform randomness. Evidence about a shuffling procedure depends on the probabilities it assigns across outcomes, not only whether one sample looks patterned.

Bayer and Diaconis’s original paper derives the rising-sequence formula and the distance calculation. Continue to Kruskal’s count, where a shuffled deck supports a different mechanism: paths merge because they share the same future instructions.

Reset all settings