# Random permutation

A realization of a random permutation of the playing cards of one suit

A random permutation or random permutation in mathematics is a random arrangement of a set of objects . For example, shuffling the cards of a deck of cards is (ideally) a random permutation of the cards. In stochastics , random permutations are viewed as uniformly distributed random variables from a discrete probability space whose values ​​are permutations. In this way, key figures of random permutations, such as the number of fixed points , deficiencies or cycles , can be viewed as discrete random variables, the distributions of which are then analyzed. In the computer, pseudo-random permutations can be efficiently generated using the Fisher-Yates method. Random permutations are examined , among other things, in the analysis of sorting processes , in cryptography and coding theory, and in the context of randomized algorithms .

## definition

If the symmetric group of all permutations of the set , then a random permutation is a uniformly distributed random variable , that is, a mapping of a probability space such that ${\ displaystyle S_ {n}}$${\ displaystyle \ {1, \ ldots, n \}}$${\ displaystyle S_ {n}}$ ${\ displaystyle \ Pi}$${\ displaystyle \ Pi \ colon \ Omega \ to S_ {n}}$ ${\ displaystyle (\ Omega, {\ mathcal {A}}, P)}$

${\ displaystyle P (\ Pi = \ pi) = {\ frac {1} {n!}}}$

holds for all permutations . More generally, permutations of any finite sets , for example alphabets , can also be considered. To analyze the mathematical properties, however, a restriction to the first natural numbers is possible. ${\ displaystyle \ pi \ in S_ {n}}$${\ displaystyle n}$

## example

The set consists of the six permutations of the numbers and , in tuple representation${\ displaystyle S_ {3}}$${\ displaystyle 1,2}$${\ displaystyle 3}$

${\ displaystyle S_ {3} = \ {(1,2,3), (1,3,2), (2,1,3), (2,3,1), (3,1,2), (3,2,1) \}}$.

A random permutation realizes each of these six permutations with the same probability ${\ displaystyle \ Pi}$${\ displaystyle \ pi \ in S_ {3}}$

${\ displaystyle P (\ Pi = \ pi) = {\ frac {1} {6}}}$.

Is as the underlying probability space with all selected, can by the identity map are displayed. ${\ displaystyle (S_ {3}, {\ mathcal {P}} (S_ {3}), P)}$${\ displaystyle P (\ {\ pi \}) = {\ tfrac {1} {6}}}$${\ displaystyle \ pi \ in S_ {3}}$${\ displaystyle \ Pi}$

## properties

Variables assigned to random permutations, such as the number of errors or cycles , can also be interpreted as discrete random variables . However, the probability distribution of these random variables is generally no longer evenly distributed. Generating functions are an important tool in the analysis of these probability distributions . If the probability function is that the random variable assumes the value , then the associated probability-generating function is carried out ${\ displaystyle X = X (\ Pi)}$${\ displaystyle P_ {X} (k) = P (X = k)}$${\ displaystyle X}$${\ displaystyle k \ in \ mathbb {N} _ {0}}$

${\ displaystyle g (x) = \ sum _ {k = 0} ^ {\ infty} P_ {X} (k) \ cdot x ^ {k}}$

Are defined. This is an exponentially generating function . The expected value of the random variable is then through ${\ displaystyle X}$

${\ displaystyle \ operatorname {E} (X) = \ sum _ {k = 0} ^ {\ infty} k \ cdot P_ {X} (k) = g '(1)}$

given and their variance accordingly

${\ displaystyle \ operatorname {Var} (X) = \ sum _ {k = 0} ^ {\ infty} (k- \ operatorname {E} (X)) ^ {2} \ cdot P_ {X} (k) = g '' (1) + g '(1) (1-g' (1))}$.

In the following, probability distributions, expected values ​​and variances of some important parameters of random permutations are given.

### Number of fixed points

Frequency distribution of the number of fixed points in a random permutation of length 7

A fixed point in a permutation is a number for which applies. The number of permutations with exactly fixed points is given by the Rencontres numbers (sequence A008290 in OEIS ). The probability-generating function of the number of fixed points in a permutation has the form ${\ displaystyle \ pi}$${\ displaystyle i \ in \ {1, \ ldots, n \}}$${\ displaystyle \ pi (i) = i}$${\ displaystyle \ pi \ in S_ {n}}$${\ displaystyle k}$ ${\ displaystyle D_ {n, k}}$${\ displaystyle \ pi \ in S_ {n}}$

${\ displaystyle g (x) = \ sum _ {k = 0} ^ {n} {\ frac {D_ {n, k}} {n!}} \, x ^ {k} = \ sum _ {k = 0} ^ {n} \ sum _ {j = 0} ^ {nk} {\ frac {(-1) ^ {j} \, x ^ {k}} {j! \, K!}}}$.

The following applies to the expected value and the variance of the number of fixed points

${\ displaystyle \ operatorname {E} (X) = \ sum _ {k = 0} ^ {n-1} {\ frac {D_ {n-1, k}} {(n-1)!}} = 1 }$

and

${\ displaystyle \ operatorname {Var} (X) = \ sum _ {k = 0} ^ {n-2} {\ frac {D_ {n-2, k}} {(n-2)!}} = 1 }$.

The number of fixed points in a random permutation is asymptotically Poisson distributed with intensity . ${\ displaystyle n \ to \ infty}$${\ displaystyle \ lambda = 1}$

### Number of climbs

Frequency distribution of the number of climbs in a random permutation of length 7

An increase in a permutation is a number for which holds. The number of permutations with exact increases (analogous to descents) is given by the Euler numbers (sequence A008292 in OEIS ). The probability generating function of the number of rises in a permutation has the form ${\ displaystyle \ pi}$${\ displaystyle i}$${\ displaystyle \ pi (i + 1)> \ pi (i)}$${\ displaystyle \ pi \ in S_ {n}}$${\ displaystyle k}$ ${\ displaystyle A_ {n, k}}$${\ displaystyle \ pi \ in S_ {n}}$

${\ displaystyle g (x) = \ sum _ {k = 0} ^ {n-1} {\ frac {A_ {n, k}} {n!}} \, x ^ {k} = {\ frac { 1} {n!}} \ Sum _ {k = 0} ^ {n-1} \ sum _ {j = 0} ^ {k} \, (- 1) ^ {j} {\ binom {n + 1 } {j}} (k + 1-j) ^ {n} \, x ^ {k}}$.

The following applies to the expected value and the variance of the number of increases

${\ displaystyle \ operatorname {E} (X) = {\ frac {n-1} {2}}}$

and

${\ displaystyle \ operatorname {Var} (X) = {\ frac {n + 1} {12}}}$.

The number of increases in a random permutation is asymptotically normally distributed with appropriate normalization for . ${\ displaystyle n \ to \ infty}$

### Number of absenteeism

Frequency distribution of the number of errors in a random permutation of length 7

A fault in a permutation is a pair for which and holds. The number of permutations with exactly missing positions is given by the McMahon numbers (sequence A008302 in OEIS ). The probability function of the number of fixed points of a permutation has the form ${\ displaystyle \ pi}$ ${\ displaystyle (i, j)}$${\ displaystyle i ${\ displaystyle \ pi (i)> \ pi (j)}$${\ displaystyle \ pi \ in S_ {n}}$${\ displaystyle k}$ ${\ displaystyle M_ {n, k}}$${\ displaystyle \ pi \ in S_ {n}}$

${\ displaystyle g (x) = \ sum _ {k = 0} ^ {n (n-1) / 2} {\ frac {M_ {n, k}} {n!}} \, x ^ {k} = {\ frac {1} {n!}} \ prod _ {i = 1} ^ {n} \ sum _ {j = 0} ^ {ni} x ^ {j} = {\ frac {1} {n !}} \ prod _ {i = 1} ^ {n} {\ frac {1-x ^ {i}} {1-x}}}$.

The following applies to the expected value and the variance of the number of deficiencies

${\ displaystyle \ operatorname {E} (X) = \ sum _ {i = 1} ^ {n} {\ frac {i-1} {2}} = {\ frac {n (n-1)} {4 }}}$

and

${\ displaystyle \ operatorname {Var} (X) = \ sum _ {i = 1} ^ {n} {\ frac {i ^ {2} -1} {12}} = {\ frac {n (2n + 5 ) (n-1)} {72}}}$.

The number of errors in a random permutation is asymptotically normally distributed with appropriate normalization for . ${\ displaystyle n \ to \ infty}$

### Number of cycles

Frequency distribution of the number of cycles in a random permutation of length 7

A cycle in a permutation is a sequence of different numbers for which for and holds. Each permutation can be completely broken down into cycles. The number of permutations with exactly cycles is given by the Stirling numbers of the first type (sequence A130534 in OEIS ). The probability generating function of the number of cycles of a permutation has the form ${\ displaystyle \ pi}$${\ displaystyle i_ {1}, i_ {2}, \ ldots, i_ {k}}$${\ displaystyle \ pi (i_ {j}) = i_ {j + 1}}$${\ displaystyle j = 1, \ ldots, k-1}$${\ displaystyle \ pi (i_ {k}) = i_ {1}}$${\ displaystyle \ pi \ in S_ {n}}$${\ displaystyle k}$${\ displaystyle s_ {n, k}}$${\ displaystyle \ pi \ in S_ {n}}$

${\ displaystyle g (x) = \ sum _ {k = 1} ^ {n} {\ frac {s_ {n, k}} {n!}} \, x ^ {k} = {\ frac {1} {n!}} \ prod _ {k = 1} ^ {n} (x + k-1)}$.

The following applies to the expected value and the variance of the number of cycles

${\ displaystyle \ operatorname {E} (X) = \ sum _ {k = 1} ^ {n} {\ frac {1} {k}} = H_ {n}}$,

where is the -th harmonic number , and ${\ displaystyle H_ {n}}$${\ displaystyle n}$

${\ displaystyle \ operatorname {Var} (X) = \ sum _ {k = 1} ^ {n} {\ frac {k-1} {k ^ {2}}} = H_ {n} -H_ {n} ^ {(2)}}$,

wherein the th harmonic number is the second order. The number of cycles in a random permutation is normally distributed asymptotically with the expected value and variance with the appropriate normalization . ${\ displaystyle H_ {n} ^ {(2)}}$${\ displaystyle n}$${\ displaystyle n \ to \ infty}$${\ displaystyle \ log n}$

## generation

An important task in the investigation of algorithms , for example sorting processes, in the context of Monte Carlo simulations is the generation of random permutations on the computer. The basic idea here is the use of uniformly distributed pseudo- random numbers with the help of suitable random number generators . These pseudo-random numbers are then combined in a suitable manner so that a pseudo-random permutation is created.

### Direct procedure

In a direct approach, a list consisting of the numbers from to is considered first. After a uniformly distributed random number between and (inclusive) has been drawn, the corresponding list element is noted as the first number of the result permutation and this element is then deleted from the list. In the next step, a uniformly distributed random number is drawn between and , the corresponding list element is noted as the second number of the result permutation and this element is also deleted again. This procedure is continued until finally there are no more numbers in the list and the result permutation is complete. This procedure can be implemented in pseudocode as follows: ${\ displaystyle 1}$${\ displaystyle n}$${\ displaystyle 1}$${\ displaystyle n}$${\ displaystyle 1}$${\ displaystyle n-1}$

function randperm(n)
P = zeroes(n)          // Ergebnispermutation mit Nullen initialisieren
N = [1:n]              // Feld mit den Zahlen von 1 bis n
for i = 1 to n         // Schleife über die Einträge von P
z = random(n-i+1)    // Gleichverteilte Zufallszahl zwischen 1 und n-i+1
P(i) = N(z)          // Wähle als nächsten Eintrag die z-te verbleibende Zahl
N = setdiff(N,N(z))  // Entferne diese Zahl aus den verbleibenden Zahlen
end
return P

Area Random number selection Result
1-6 5 1 2 3 4 5 6 5
1-5 3 1 2 3 4 6 5 3
1-4 4th 1 2 4 6 5 3 6
1-3 1 1 2 4 5 3 6 1
1-2 2 2 4 5 3 6 1 4
1-1 1 2 5 3 6 1 4 2

In this process, the places are passed through one after the other, with each place being assigned a random number selected from the numbers still available. Alternatively, the dual approach can be followed, in which the numbers are run through one after the other, with each number being assigned a randomly selected free space. In both cases, the efficiency of the method suffers from the fact that it is time-consuming to remove a certain element from a list or to find a certain free space. In a standard implementation, these subtasks require operations on average , so that the overall process has a runtime complexity of order${\ displaystyle O (n)}$

${\ displaystyle O (n ^ {2})}$

having. The adjacent table shows an example of how this method works when generating a pseudo-random permutation of length six. The number selected and thus eliminated in each step is shown with a line through it.

### Fisher-Yates method

The Fisher-Yates method (according to Ronald Fisher and Frank Yates ) represents an improvement . This method also works in place , that is, given a preinitialized field with the numbers from to , these numbers are gradually rearranged so that at the end a random one Permutation arises. The time-consuming search for an unused number to avoid, in each step, the selected number will be the end of the currently considered subfield by the last remaining non-selected number swapped is. In pseudocode, this procedure is as follows: ${\ displaystyle 1}$${\ displaystyle n}$

function randperm(n)
P = [1:n]             // Initialisierung mit der identischen Permutation
for i = n downto 1    // Schleife über die Einträge von P
z = random(i)       // Gleichverteilte Zufallszahl zwischen 1 und i
swap(P(i),P(z))     // Vertausche die Zahlen an den Stellen i und z
end
return P

Area Random number permutation
1-6 5 1 2 3 4 5 6
1-5 3 1 2 3 4 6 5
1-4 4th 1 2 6 4 3 5
1-3 1 1 2 6 4 3 5
1-2 2 6 2 1 4 3 5
1-1 1 6 2 1 4 3 5

As in the code example, the initialization can take place with the identical permutation , but any permutation can also be used here, which is then re-sorted accordingly. This makes the method particularly attractive for simulation applications in which the permutation routine is called iteratively . Since the interchanging of two elements of a field of fixed size takes place with constant effort, the Fisher-Yates method has a runtime complexity of the order

${\ displaystyle O (n)}$,

which is a significant improvement over the direct method. In the adjacent table, the method is also illustrated using an example in which a pseudo-random permutation of length six is ​​generated again. The numbers that have been swapped with one another are shown underlined. The last step can also be omitted, as there is never a change here. The same random numbers were used as in the example for the direct method, but the result permutation is different here.

This method is provided as a built-in function in the numerical software package MATLAB , for example randperm.

• 100 Prisoners Problem , a mathematical puzzle based on random permutations
• RC4 , a stream encryption method that uses random permutations
• Bogosort , an inefficient sorting method that also uses random permutations
• Linear congruence generator , a random number generator that generates a pseudo-random permutation with a maximum period length
• Golomb-Dickman constant , the expectation of the relative length of the longest cycle in a random permutation for${\ displaystyle n \ to \ infty}$