262009/16: Cards left in place by a fair shuffle

26.1Problem

On the website http://www.pokerstars.hu/poker/room/features/security/ a summary can be read related to problems of algorithm of evenhanded shuffle. Here a shuffle algorithm can be found which is said to be reliable and is free of ‘bad distribution of shuffle’. Let be given deck of \(N\) cards which is mixed by the algorithm found on this website! Let investigate, how many cards remain in its original place after mixing. What is the probability, \(P_{N,k}\) of that exactly \(0 \le k \le N\) cards remained in its original place? What is the expected value and standard deviation of \(k\) for values of \(N\)? Calculate the entropy of shuffle, \(S(N) = -\sum_{k=0}^{N} P_{N,k}\log_2 P_{N,k}\)! Give the value of \(P_{N,0}\) in the case of \(N \to \infty\) and estimate the difference between \(P_{N,0}\) and this limit!

(Gábor Homa and Krisztián Kis-Szabó)

26.2Explanation

We read the web page cited in the problem in the copies that the Internet Archive kept from 2009 [68]. The page describes the shuffle as follows: draw a card at random from the original deck of \(N\) cards and put it into a new deck; draw a second card at random from the \(N-1\) remaining cards and put it on top of the new deck; repeat until the original deck is empty. Every random draw is made with equal probabilities, from random bits obtained from hardware noise and user input. We assume these bits are ideal, that is independent and equally likely to be 0 or 1. We number the positions in both decks from the top and say that a card stays in place if its position in the new deck is the same as in the original deck. We have to find the distribution \(P_{N,k}\) of the number \(k\) of cards that stay in place, its mean and standard deviation, the entropy \(S(N)\) of this distribution, the limit of \(P_{N,0}\) for \(N \to \infty\), and how fast \(P_{N,0}\) approaches it. The “entropy of shuffle” is defined in the problem as the entropy of the distribution of \(k\) alone; it is not the entropy of the shuffled order of the deck, and we compare the two in the solution. The page claims that the shuffle is free of the “bad distribution of shuffles” described in an earlier article [69]; at the end we show what that defect is.

26.3Theory

Permutations. An ordering of \(N\) different cards is a permutation. There are \(N! = N(N-1)\cdots 2\cdot 1\) of them: \(N\) choices for the top card, \(N-1\) for the next one, and so on. A shuffle is uniform if each of the \(N!\) orders comes out with the same probability \(1/N!\). For a deck of 52 cards \(52! = 8.07\times 10^{67}\). A card that is in the same position before and after the shuffle is a fixed point of the permutation. A permutation without fixed points is a derangement, and the number of derangements of \(m\) cards is denoted by \(D_m\).

Mean and variance. If a quantity \(k\) takes the value \(k\) with the probability \(P_k\), its mean (expected value) is \(\langle k\rangle = \sum_k k P_k\), its variance is \(\sigma^2 = \langle (k - \langle k\rangle)^2\rangle = \langle k^2\rangle - \langle k\rangle^2\), and its standard deviation is \(\sigma\). The mean of a sum is the sum of the means, \(\langle X + Y\rangle = \langle X\rangle + \langle Y\rangle\), even if \(X\) and \(Y\) are not independent. We use this with indicator quantities: \(X_i = 1\) if a given event happens and \(X_i = 0\) otherwise, so that \(\langle X_i\rangle\) is the probability of the event, and a sum of indicators counts how many of the events happen.

Inclusion–exclusion. To count the elements of a union of sets \(A_1, \dots, A_N\) we add the sizes of the sets, subtract the sizes of all pairwise intersections (whose elements were counted twice), add the sizes of all triple intersections, and so on:

\[ |A_1 \cup \dots \cup A_N| = \sum_i |A_i| - \sum_{i<j} |A_i \cap A_j| + \sum_{i<j<l} |A_i \cap A_j \cap A_l| - \dots \tag{178}\]

An element that belongs to exactly \(r \ge 1\) of the sets is counted \(\binom{r}{1} - \binom{r}{2} + \binom{r}{3} - \dots = 1 - (1-1)^r = 1\) times, so the formula is correct.

Alternating series. The exponential function has the series \(e^x = \sum_{j=0}^{\infty} x^j/j!\), so \(e^{-1} = 1 - 1 + \tfrac{1}{2} - \tfrac{1}{6} + \tfrac{1}{24} - \dots\). If the terms of a series alternate in sign and decrease in magnitude, the partial sums jump back and forth around the limit, and the error of a partial sum has the sign of the first omitted term and is smaller than it in magnitude.

Entropy. The entropy of a probability distribution \(P_k\) is \(S = -\sum_k P_k \log_2 P_k\), measured in bits (terms with \(P_k = 0\) contribute zero). It is zero for a certain outcome and \(\log_2 M\) for \(M\) equally likely outcomes; roughly, it is the average number of yes-or-no questions needed to find out the outcome. The physicist’s entropy of statistical mechanics is the same expression with the natural logarithm, multiplied by the Boltzmann constant.

The Poisson distribution. A quantity \(k = 0, 1, 2, \dots\) has the Poisson distribution with mean \(\lambda\) if \(P_k = \lambda^k e^{-\lambda}/k!\). Its mean and its variance are both \(\lambda\). It describes the number of events when there are many independent chances, each with a small probability, such as the number of decays per second in a radioactive sample.

26.4Solutions

26.4.1Counting permutations

The shuffle is uniform. Consider one particular final order of the new deck. It arises from exactly one sequence of draws: the card at the bottom of the new deck must be drawn first, the card above it second, and so on. The first draw is right with the probability \(1/N\), the second with \(1/(N-1)\), and the last with \(1/1\), so the order has the probability

\[ \frac{1}{N}\cdot\frac{1}{N-1}\cdots\frac{1}{1} = \frac{1}{N!} , \]

the same for every order. The shuffle is therefore uniform, provided that each single draw is uniform. The page explains how this is ensured. To pick one of 26 cards, five random bits give a number from 0 to 31; if the number is above 25 it is discarded and five new bits are taken. Each of the values 0 to 25 then has the probability \(1/26\). Taking the five-bit number modulo 26 instead would give the values 0 to 5 twice the probability of the others. The same shuffle is usually programmed without a second deck, as the Fisher–Yates shuffle [70]: for \(t = 1, \dots, N-1\), swap the card in position \(t\) with a card chosen uniformly from positions \(t, t+1, \dots, N\).

Since every order has the probability \(1/N!\), every question about the shuffle is a counting question: \(P_{N,k}\) is the number of permutations of \(N\) cards with exactly \(k\) fixed points, divided by \(N!\).

Derangements. Let \(A_i\) be the set of permutations in which card \(i\) stays in place. A permutation that keeps \(j\) given cards in place may order the other \(N - j\) cards arbitrarily, so the intersection of any \(j\) of the sets has \((N-j)!\) elements, and there are \(\binom{N}{j}\) such intersections. The permutations with at least one fixed point form the union of the \(A_i\), and by (178) the number of derangements is

\[ D_N = N! - \sum_{j=1}^{N} (-1)^{j-1}\binom{N}{j}(N-j)! = \sum_{j=0}^{N} (-1)^j \frac{N!}{j!} = N!\left(1 - 1 + \frac{1}{2!} - \dots + \frac{(-1)^N}{N!}\right) . \tag{179}\]

The first values, from \(D_0 = 1\) (the empty deck has no fixed point) to \(D_{10}\), are 1, 0, 1, 2, 9, 44, 265, 1854, 14 833, 133 496 and 1 334 961. For three cards \(A, B, C\), for example, the two derangements are \(BCA\) and \(CAB\).

The probability of \(k\) fixed points. A permutation with exactly \(k\) fixed points is specified by choosing the \(k\) cards that stay in place, in \(\binom{N}{k}\) ways, and a derangement of the other \(N-k\) cards, in \(D_{N-k}\) ways. Dividing by \(N!\) and using (179),

\[ P_{N,k} = \binom{N}{k}\frac{D_{N-k}}{N!} = \frac{D_{N-k}}{k!\,(N-k)!} = \frac{1}{k!}\sum_{j=0}^{N-k}\frac{(-1)^j}{j!} , \qquad k = 0, 1, \dots, N . \tag{180}\]

For \(N = 3\) this gives \(P_{3,0} = 1/3\), \(P_{3,1} = 1/2\), \(P_{3,2} = 0\) and \(P_{3,3} = 1/6\), and for \(N = 4\) it gives \(3/8\), \(1/3\), \(1/4\), \(0\) and \(1/24\). The probability \(P_{N,N-1}\) is always zero, because if \(N-1\) cards are in place the last card has only its own place left. The numbers \(\binom{N}{k}D_{N-k}\) are called the rencontres numbers. Formula (180) also shows that \(P_{N,k} = P_{N-k,0}/k!\). As \(N \to \infty\) with \(k\) fixed, the sum becomes the series of \(e^{-1}\), so

\[ P_{N,k} \to \frac{e^{-1}}{k!} , \tag{181}\]

the Poisson distribution with mean 1. The reason is the one given in the Theory section for the Poisson distribution: each card stays in place with the small probability \(1/N\), and for large \(N\) these \(N\) events are almost independent. If they were exactly independent, the probability that no card stays in place would be \((1 - 1/N)^N\), which also tends to \(e^{-1}\).

Mean and standard deviation. Let \(X_i = 1\) if card \(i\) stays in place and \(X_i = 0\) otherwise, so that \(k = X_1 + \dots + X_N\). Card \(i\) is equally likely to end in any of the \(N\) positions, so \(\langle X_i\rangle = 1/N\), and

\[ \langle k\rangle = N\cdot\frac{1}{N} = 1 . \]

For two different cards \(i \ne j\), the product \(X_i X_j\) is 1 only if both stay in place, which happens in \((N-2)!\) of the \(N!\) orders, so \(\langle X_i X_j\rangle = 1/(N(N-1))\). Since \(X_i^2 = X_i\),

\[ \langle k^2\rangle = \sum_i \langle X_i\rangle + \sum_{i \ne j}\langle X_i X_j\rangle = 1 + N(N-1)\cdot\frac{1}{N(N-1)} = 2 , \qquad \sigma^2 = \langle k^2\rangle - \langle k\rangle^2 = 1 . \]

For every \(N \ge 2\) the mean number of cards left in place is therefore exactly 1 and the standard deviation is exactly 1, independently of the size of the deck. The case \(N = 1\) is the exception: the single card always stays in place, so \(k = 1\) and \(\sigma = 0\). The values agree with the Poisson limit (181), whose mean and variance are both 1. The same calculation for \(r\) cards shows that \(\langle k(k-1)\cdots(k-r+1)\rangle = 1\) for every \(r \le N\), which is also true for the Poisson distribution with mean 1; for \(r > N\) the left-hand side is 0.

Entropy. With (180), \(S(N) = -\sum_k P_{N,k}\log_2 P_{N,k}\) is a finite sum, and Table 19 lists its values. For \(N = 1\) the outcome is certain and \(S(1) = 0\). For \(N = 2\) there are two equally likely outcomes, \(k = 0\) and \(k = 2\), and \(S(2) = 1\) bit. For \(N = 3\), \(S(3) = \tfrac13\log_2 3 + \tfrac12\log_2 2 + \tfrac16\log_2 6 = 1.459\) bits. As \(N\) grows, \(S(N)\) approaches from below the entropy of the Poisson distribution (181). With \(-\log_2(e^{-1}/k!) = (1 + \ln k!)/\ln 2\),

\[ S(\infty) = \frac{1}{\ln 2}\left(1 + e^{-1}\sum_{k=2}^{\infty}\frac{\ln k!}{k!}\right) = 1.882489 \text{ bits}. \]

The approach is fast: \(S(8)\) is below the limit by \(1.9\times 10^{-4}\) and \(S(10)\) by \(3.0\times 10^{-6}\), and for \(N = 52\) the two agree to all digits a computer represents. This entropy is small because it describes only the number \(k\), which is 0, 1 or 2 in 92% of the shuffles. The entropy of the shuffled order itself, \(N!\) equally likely outcomes, is \(\log_2 N!\), which is \(225.58\) bits for \(N = 52\). This is why the page uses 249 random bits per shuffle: fewer than 226 bits cannot produce all \(52!\) orders.

Table 19: The probability that no card stays in place, its distance from the limit \(1/e\), the same distance in units of \(1/(N+1)!\), and the entropy of the distribution of \(k\), all for the uniform shuffle.
\(N\)\(P_{N,0}\)\(P_{N,0} - 1/e\)\((N+1)!\,|P_{N,0} - 1/e|\)\(S(N)\) (bits)
10\(-3.68\times 10^{-1}\)0.7360
20.5\(+1.32\times 10^{-1}\)0.7931
30.333333\(-3.45\times 10^{-2}\)0.8291.459148
40.375000\(+7.12\times 10^{-3}\)0.8541.750000
50.366667\(-1.21\times 10^{-3}\)0.8731.848505
60.368056\(+1.76\times 10^{-4}\)0.8881.875491
70.367857\(-2.23\times 10^{-5}\)0.8991.881256
80.367882\(+2.50\times 10^{-6}\)0.9081.882302
90.367879\(-2.52\times 10^{-7}\)0.9161.882464
100.367879\(+2.31\times 10^{-8}\)0.9231.882486
520.367879\(+2.30\times 10^{-70}\)0.9821.882489
\(\infty\)0.367879011.882489

The limit of \(P_{N,0}\) and the error. By (180) with \(k = 0\), \(P_{N,0} = \sum_{j=0}^{N}(-1)^j/j!\) is a partial sum of the series of \(e^{-1}\), so

\[ \lim_{N\to\infty} P_{N,0} = \frac{1}{e} = 0.3678794412 . \]

The difference is the omitted tail of the series. Taking out the first omitted term,

\[ P_{N,0} - \frac{1}{e} = -\sum_{j=N+1}^{\infty}\frac{(-1)^j}{j!} = \frac{(-1)^N}{(N+1)!}\left(1 - \frac{1}{N+2} + \frac{1}{(N+2)(N+3)} - \dots\right) . \tag{182}\]

The bracket is again an alternating series with decreasing terms, so it lies between \(1 - 1/(N+2)\) and 1, and

\[ \frac{N+1}{N+2}\cdot\frac{1}{(N+1)!} < \left|P_{N,0} - \frac{1}{e}\right| < \frac{1}{(N+1)!} . \]

The error therefore decreases faster than any power of \(N\), and its sign alternates: \(P_{N,0}\) is above \(1/e\) for even \(N\) and below it for odd \(N\) (Table 19). For a deck of 4 cards the difference is \(7.1\times 10^{-3}\), for 10 cards \(2.3\times 10^{-8}\), and for 52 cards it is \(2.30\times 10^{-70}\), while \(1/53! = 2.34\times 10^{-70}\). Since the error is below \(1/2\) even after multiplying by \(N!\), the number of derangements is the integer nearest to \(N!/e\) for every \(N \ge 1\). The other probabilities converge in the same way: by \(P_{N,k} = P_{N-k,0}/k!\), the difference \(|P_{N,k} - e^{-1}/k!|\) is smaller than \(1/(k!\,(N-k+1)!)\).

26.4.2Enumeration and simulation

Two numerical checks are independent of the counting argument. The first lists all \(N!\) permutations of \(N = 1, \dots, 9\) cards and counts their fixed points. For \(N = 9\) the \(362\,880\) permutations split as \(133\,496\), \(133\,497\), \(66\,744\), \(22\,260\), \(5\,544\), \(1\,134\), \(168\), \(36\), \(0\) and \(1\) for \(k = 0, \dots, 9\), which agrees with \(\binom{9}{k}D_{9-k}\) for every \(k\); the same holds for all smaller \(N\), and the exact means and variances computed from these distributions are 1 and 1 for every \(N \ge 2\).

The second check simulates the algorithm exactly as the page describes it, with two decks, for \(N = 52\). The random draws come from the pseudo-random generator of the Python language, which also picks a number from \(0\) to \(n-1\) by discarding out-of-range values of random bits. In \(100\,000\) shuffles the mean number of cards left in place was \(0.9974\) and the standard deviation \(0.9986\); the expected statistical error of the mean is \(1/\sqrt{100\,000} = 0.0032\), so both agree with the exact value 1. The observed frequencies of \(k = 0, 1, 2, 3\) were \(0.3686\), \(0.3683\), \(0.1836\) and \(0.0604\), against \(0.3679\), \(0.3679\), \(0.1839\) and \(0.0613\) from (180), and the largest \(k\) observed was 8. A separate run of \(240\,000\) shuffles of 4 cards produced all 24 orders, each between \(9\,787\) and \(10\,231\) times, consistent with \(10\,000\) each and a statistical spread of about \(\sqrt{10\,000} = 100\).

26.4.3Incorrect shuffles

The naive swap. The article cited on the page [69] analyzed the shuffle that a popular online poker room used and published in 1999. Its core is the following loop: for \(t = 1, \dots, N\), swap the card in position \(t\) with a card chosen uniformly from all \(N\) positions. It looks as random as the Fisher–Yates shuffle, which chooses only from positions \(t, \dots, N\), but it is not uniform. The \(N\) choices form \(N^N\) equally likely sequences, and each sequence produces one of the \(N!\) orders. For \(N \ge 3\) the number \(N-1\) divides \(N!\) but not \(N^N\), because \(N-1\) and \(N\) have no common factor, so \(N^N/N!\) is not an integer and the orders cannot all receive the same number of sequences. For \(N = 3\), three orders arise from 5 of the 27 sequences and three from 4.

We computed the exact distribution of the naive shuffle for \(N \le 8\) by following all \(N^N\) choice sequences, combining sequences that lead to the same order after each step. Table 20 and Figure 49 show the result. The most likely order is \(15.1\) times as likely as the least likely one for \(N = 8\), and the ratio grows with \(N\). The distribution of the number of cards left in place is visibly wrong: for \(N = 8\) the probability that no card stays in place is \(0.414\) instead of \(0.368\), and the mean is \(0.920\) instead of 1.

Table 20: The naive swap shuffle, computed exactly from all \(N^N\) choice sequences; \(p_{\max}/p_{\min}\) is the ratio of the largest and the smallest probability of an order. For the uniform shuffle \(\langle k\rangle = \sigma = 1\), and \(S(N)\) is in Table 19.
\(N\)\(P_{N,0}\) naive\(P_{N,0}\) uniform\(\langle k\rangle\) naive\(\sigma\) naive\(S(N)\) naive\(p_{\max}/p_{\min}\)
30.33330.33330.96300.96151.42781.250
40.39060.37500.94530.97871.72231.875
50.39580.36670.93500.98791.80432.938
60.40500.36810.92830.99351.83214.969
70.41000.36790.92360.99721.83768.484
80.41370.36790.92010.99991.838715.086

The defect does not disappear for large decks. For \(N = 52\) the full distribution cannot be listed, but the mean can be computed exactly by following one card at a time: the probabilities of the \(N\) possible positions of a card form a list of \(N\) numbers, and each swap changes the list in a known way. This gives \(\langle k\rangle = 0.8999\) for the naive shuffle of 52 cards, against 1 for a uniform shuffle. The result has a closed form: with \(a = 1 - 1/N\), card \(i\) ends in its own place with the probability \((a^{N-i} + a^{i-1} - a^{N-1})/N\), and summing over \(i\) gives \(\langle k\rangle = 2(1 - a^N) - a^{N-1}\), both of which agree exactly with the step-by-step computation for \(N \le 8\) and \(N = 52\). For \(N \to \infty\) it tends to \(2 - 3/e = 0.8964\), not to 1. A simulation of \(100\,000\) naive shuffles of 52 cards gave the mean \(0.8992\), the standard deviation \(1.015\), and the frequency \(0.434\) for \(k = 0\), against \(1/e = 0.368\) for a uniform shuffle; the statistical error of the frequency is about \(0.0016\).

Figure 49
Figure 49: The probability that exactly \(k\) of 8 cards stay in place, for the uniform shuffle described on the PokerStars page (blue) and for the naive swap shuffle (orange). Both are exact. The naive shuffle leaves no card in place too often and one card in place too rarely.

Two other errors. The published program had a second error: the random position was chosen from \(1, \dots, N-1\) only, so the card in position \(N\) is never touched before the last step, and in the last step it is swapped to one of the positions \(1, \dots, N-1\). The last card can therefore never stay in place [69], and the \((N-1)!\) orders that keep it there never occur; our enumeration for \(N \le 7\) confirms this. A third common error is to choose the swap partner of position \(t\) from positions \(t+1, \dots, N\), excluding \(t\) itself. This is Sattolo’s algorithm [71], which produces each of the \((N-1)!\) permutations that consist of a single cycle with equal probability. In such a permutation no card stays in place, so \(k = 0\) in every shuffle. The number of cards left in place is therefore a useful test of a shuffle program: it is easy to measure, and for a uniform shuffle its distribution is known exactly.

References

  • [68] PokerStars. Security (web page), copy of 8 November 2009 in the Internet Archive. web.archive.org/web/20091108170123/ pokerstars.hu/poker/room/features/security/
  • [69] B. Arkin, F. Hill, S. Marks, M. Schmid, T. J. Walls and G. McGraw. How we learned to cheat at online poker: a study in software security. developer.com, 28 September 1999.
  • [70] D. E. Knuth. The Art of Computer Programming, Volume 2: Seminumerical Algorithms. Addison-Wesley, 3rd edition, 1997, Section 3.4.2.
  • [71] S. Sattolo. An algorithm to generate a random cyclic permutation. Information Processing Letters 22, 315–317, 1986.