We'll each take a 52-card deck, shuffle it thoroughly and place our decks
face down on the table. Now we'll start turning cards face up, one from
the top of each deck on each turn. If we go through the entire deck
without two identical cards being turned, player A wins. If an identical
pair is turned, player B wins. Do you want to be player A or
B?

Some knowledge of derangements may help you choose.

A** derangement ** of a set of numbers is a permutation in which no number
appears in its natural order. So, for example,
{5,4,1,2,3} is a derangement of {1,2,3,4,5}.
The number of derangements for a set of **n** objects is sometimes call the
subfactorial of **n** and denoted as **!n**.

There are number of algorithms for evaluating **!n**. The
simplest is derived from the equation: **!n= n! x sum**_{k=0 to n }((-1)^{k}/k!)
where** n! **is **n** factorial, the product of integers **1**
through **n**. It just so happens that the series
represented by **sum**_{k=0 to n }((-1)^{k}/k!)
approaches **1/e** as **k** approaches infinity (yup, the
same irrational number **e **(2.718281...) that is the base of natural
logarithms).

So the easy way to calculate **!n** is as **round(n!/e)**, **n**
factorial divided by **e** rounded to the nearest integer.

Delphi does not have **e** as a predefined constant (it does have **pi**,
but discriminates against **e** for some reason). It has the** Exp(x)**
function, **e** raised to the **x **power, though. So defining
**e** as **exp(1.0)** is pretty trivial.

Back to the original card game problem. The "natural order"
of a set can be any predetermined order. In this case we'll let deck
number 1 define the natural order, and deck number 2 will be a derangement if no
card in deck 2 matches it's position in deck 1. Player A wins
in this case, Player B wins in deck 2 is not a derangement of deck
1. The probability of an event is the number of
"successful" outcomes divided by the total number of possible
outcomes. In this case the total possible outcomes number **52!**
and the successful (derangement) outcomes is **round(52!/e)** which will be
very close to **52!/e**. So the probability of no match will be
close to **(52!/e)/52! = 1/e** or about **1/2.71828=0.36788**.
We can therefore conclude that A has about a 37% chance of winning and, since
somebody will win, B has a 63% chance of winning. I did
the proof, so I get to choose first whether to be A or B and you are probably
going to lose!

According to our Big Factorials program
52! has 68 digits, so I used a smaller problem in the program to reach the same
conclusion. The program provided, calculates actual derangement counts for
N in the range of 1 to 10 and points out that the probability approaches 1/e as
N gets larger.

The program will also display the first 100 derangements for the selected
N. I could find no reference to a function that would generate
derangements directly. So I fell back on our old faithful **UCombos**
unit to generate permutations and then just weed out those that are not
derangements.

Download source code

Download executable program