NIST

Fisher-Yates shuffle

(algorithm)

Definition: Randomly permute N elements by exchanging each element ei with a random element from i to N. It consumes Θ(N log N) bits and runs in linear time.

Generalization (I am a kind of ...)
perfect shuffle, permutation.

See also Johnson-Trotter, pseudo-random number generator.

Note: The algorithm can be viewed as a reverse selection sort. It is described in some detail as algorithm P in [Knuth98, vol. 2, p 145].

Author: PEB

Implementation

Ben Pfaff's answer to how can I shuffle the contents of an array? (C). An implementation (Java) due to Sedgewick and Wayne, including an explanation of unfair shuffle algorithms (search for Shuffling).

More information

R. A. Fisher and F. Yates, Example 12, Statistical Tables, London, 1938.
Richard Durstenfeld, Algorithm 235: Random permutation, CACM 7(7):420, July 1964.


Go to the Dictionary of Algorithms and Data Structures home page.

If you have suggestions, corrections, or comments, please get in touch with Paul E. Black.

Entry modified 14 August 2008.
HTML page formatted Thu Aug 14 12:18:08 2008.

Cite this as:
Paul E. Black, "Fisher-Yates shuffle", in Dictionary of Algorithms and Data Structures [online], Paul E. Black, ed., U.S. National Institute of Standards and Technology. 14 August 2008. (accessed TODAY) Available from: http://www.nist.gov/dads/HTML/fisherYatesShuffle.html

to NIST home page