A random permutation is a
random
In common usage, randomness is the apparent or actual lack of pattern or predictability in events. A random sequence of events, symbols or steps often has no :wikt:order, order and does not follow an intelligible pattern or combination. Ind ...
ordering of a set of objects, that is, a
permutation
In mathematics, a permutation of a set is, loosely speaking, an arrangement of its members into a sequence or linear order, or if the set is already ordered, a rearrangement of its elements. The word "permutation" also refers to the act or proc ...
-valued
random variable
A random variable (also called random quantity, aleatory variable, or stochastic variable) is a mathematical formalization of a quantity or object which depends on random events. It is a mapping or a function from possible outcomes (e.g., the po ...
. The use of random permutations is often fundamental to fields that use
randomized algorithm
A randomized algorithm is an algorithm that employs a degree of randomness as part of its logic or procedure. The algorithm typically uses uniformly random bits as an auxiliary input to guide its behavior, in the hope of achieving good performan ...
s such as
coding theory
Coding theory is the study of the properties of codes and their respective fitness for specific applications. Codes are used for data compression, cryptography, error detection and correction, data transmission and data storage. Codes are stud ...
,
cryptography
Cryptography, or cryptology (from grc, , translit=kryptós "hidden, secret"; and ''graphein'', "to write", or ''-logia'', "study", respectively), is the practice and study of techniques for secure communication in the presence of adver ...
, and
simulation
A simulation is the imitation of the operation of a real-world process or system over time. Simulations require the use of Conceptual model, models; the model represents the key characteristics or behaviors of the selected system or proc ...
. A good example of a random permutation is the
shuffling
Shuffling is a procedure used to randomize a deck of playing cards to provide an element of chance in card games. Shuffling is often followed by a cut, to help ensure that the shuffler has not manipulated the outcome.
__TOC__
Techniques
Overha ...
of a
deck of cards
A playing card is a piece of specially prepared card stock, heavy paper, thin cardboard, plastic-coated paper, cotton-paper blend, or thin plastic that is marked with distinguishing motifs. Often the front (face) and back of each card has a fi ...
: this is ideally a random permutation of the 52 cards.
Generating random permutations
Entry-by-entry brute force method
One method of generating a random permutation of a set of length ''n''
uniformly at random (i.e., each of the ''n''!
permutation
In mathematics, a permutation of a set is, loosely speaking, an arrangement of its members into a sequence or linear order, or if the set is already ordered, a rearrangement of its elements. The word "permutation" also refers to the act or proc ...
s is equally likely to appear) is to generate a
sequence
In mathematics, a sequence is an enumerated collection of objects in which repetitions are allowed and order matters. Like a set, it contains members (also called ''elements'', or ''terms''). The number of elements (possibly infinite) is calle ...
by taking a random number between 1 and ''n'' sequentially, ensuring that there is no repetition, and interpreting this sequence (''x''
1, ..., ''x''
''n'') as the permutation
:
shown here in
two-line notation.
This brute-force method will require occasional retries whenever the random number picked is a repeat of a number already selected. This can be avoided if, on the ''i''th step (when ''x''
1, ..., ''x''
''i'' − 1 have already been chosen), one chooses a number ''j'' at random between 1 and ''n'' − ''i'' + 1 and sets ''x''
''i'' equal to the ''j''th largest of the unchosen numbers.
Fisher-Yates shuffles
A simple
algorithm
In mathematics and computer science, an algorithm () is a finite sequence of rigorous instructions, typically used to solve a class of specific Computational problem, problems or to perform a computation. Algorithms are used as specificat ...
to generate a permutation of ''n'' items uniformly at random without retries, known as the
Fisher–Yates shuffle
The Fisher–Yates shuffle is an algorithm for generating a random permutation of a finite sequence—in plain terms, the algorithm shuffling, shuffles the sequence. The algorithm effectively puts all the elements into a hat; it continually deter ...
, is to start with any permutation (for example, the
identity permutation
In mathematics, a permutation group is a group ''G'' whose elements are permutations of a given set ''M'' and whose group operation is the composition of permutations in ''G'' (which are thought of as bijective functions from the set ''M'' to it ...
), and then go through the positions 0 through ''n'' − 2 (we use a convention where the first element has index 0, and the last element has index ''n'' − 1), and for each position ''i'' swap the element currently there with a randomly chosen element from positions ''i'' through ''n'' − 1 (the end), inclusive. It's easy to verify that any permutation of ''n'' elements will be produced by this algorithm with probability exactly 1/''n''!, thus yielding a uniform distribution over all such permutations.
unsigned uniform(unsigned m); /* Returns a random integer 0 <= uniform(m) <= m-1 with uniform distribution */
void initialize_and_permute(unsigned permutation[], unsigned n)
Note that if the
uniform()
function is implemented simply as
random() % (m)
then a bias in the results is introduced if the number of return values of
random()
is not a multiple of m, but this becomes insignificant if the number of return values of
random()
is orders of magnitude greater than m.
Statistics on random permutations
Fixed points
The
probability distribution
In probability theory and statistics, a probability distribution is the mathematical function that gives the probabilities of occurrence of different possible outcomes for an experiment. It is a mathematical description of a random phenomenon i ...
of the number of
fixed points in a uniformly distributed random permutation approaches a
Poisson distribution
In probability theory and statistics, the Poisson distribution is a discrete probability distribution that expresses the probability of a given number of events occurring in a fixed interval of time or space if these events occur with a known co ...
with
expected value
In probability theory, the expected value (also called expectation, expectancy, mathematical expectation, mean, average, or first moment) is a generalization of the weighted average. Informally, the expected value is the arithmetic mean of a l ...
1 as ''n'' grows. In particular, it is an elegant application of the
inclusion–exclusion principle
In combinatorics, a branch of mathematics, the inclusion–exclusion principle is a counting technique which generalizes the familiar method of obtaining the number of elements in the union of two finite sets; symbolically expressed as
: , A \cup ...
to show that the probability that there are no fixed points approaches 1/''e''. When ''n'' is big enough, the
probability distribution
In probability theory and statistics, a probability distribution is the mathematical function that gives the probabilities of occurrence of different possible outcomes for an experiment. It is a mathematical description of a random phenomenon i ...
of
fixed points is almost the
Poisson distribution
In probability theory and statistics, the Poisson distribution is a discrete probability distribution that expresses the probability of a given number of events occurring in a fixed interval of time or space if these events occur with a known co ...
with
expected value
In probability theory, the expected value (also called expectation, expectancy, mathematical expectation, mean, average, or first moment) is a generalization of the weighted average. Informally, the expected value is the arithmetic mean of a l ...
1.
The first ''n''
moments of this distribution are exactly those of the Poisson distribution.
Randomness testing
As with all random processes, the quality of the resulting distribution of an implementation of a randomized algorithm such as the Knuth shuffle (i.e., how close it is to the desired uniform distribution) depends on the quality of the underlying source of randomness, such as a
pseudorandom number generator
A pseudorandom number generator (PRNG), also known as a deterministic random bit generator (DRBG), is an algorithm for generating a sequence of numbers whose properties approximate the properties of sequences of random numbers. The PRNG-generate ...
. There are many possible
randomness test
A randomness test (or test for randomness), in data evaluation, is a test used to analyze the distribution of a set of data to see if it can be described as random (patternless). In stochastic modeling, as in some computer simulations, the hoped ...
s for random permutations, such as some of the
Diehard tests The diehard tests are a battery of statistical tests for measuring the quality of a random number generator. They were developed by George Marsaglia over several years and first published in 1995 on a CD-ROM of random numbers.
Test overview
; Bi ...
. A typical example of such a test is to take some
permutation statistic
The statistics of random permutations, such as the cycle structure of a random permutation are of fundamental importance in the analysis of algorithms, especially of sorting algorithms, which operate on random permutations. Suppose, for example, ...
for which the distribution is known and test whether the distribution of this statistic on a set of randomly generated permutations closely approximates the true distribution.
See also
*
Ewens's sampling formula
In population genetics, Ewens's sampling formula, describes the probabilities associated with counts of how many different alleles are observed a given number of times in the sample.
Definition
Ewens's sampling formula, introduced by Warren Ewens ...
— a connection with
population genetics
Population genetics is a subfield of genetics that deals with genetic differences within and between populations, and is a part of evolutionary biology. Studies in this branch of biology examine such phenomena as adaptation, speciation, and pop ...
*
Faro shuffle
The faro shuffle (American), weave shuffle (British), or dovetail shuffle is a method of shuffling playing cards, in which half of the deck is held in each hand with the thumbs inward, then cards are released by the thumbs so that they fall to the ...
*
Golomb–Dickman constant In mathematics, the Golomb–Dickman constant arises in the theory of random permutations and in number theory. Its value is
:\lambda = 0.62432 99885 43550 87099 29363 83100 83724\dots
It is not known whether this constant is rational or irration ...
*
Random permutation statistics
The statistics of random permutations, such as the cycle structure of a random permutation are of fundamental importance in the analysis of algorithms, especially of sorting algorithms, which operate on random permutations. Suppose, for example, t ...
*
Shuffling algorithms — random sort method, iterative exchange method
*
Pseudorandom permutation
References
{{Reflist
External links
Random permutationat
MathWorld
''MathWorld'' is an online mathematics reference work, created and largely written by Eric W. Weisstein. It is sponsored by and licensed to Wolfram Research, Inc. and was partially funded by the National Science Foundation's National Science Dig ...
Random permutation generation-- detailed and practical explanation of Knuth shuffle algorithm and its variants for generating ''k''-permutations (permutations of ''k'' elements chosen from a list) and ''k''-subsets (generating a subset of the elements in the list without replacement) with pseudocode
Permutations
Randomized algorithms