Random permutation
ALGORITHM
SHUFFLE applies a random permutation to the A.1,...,A.N array.
IMPLEMENTATION
Unit: internal subroutine
Global variables: array A. of arbitrary elements
Parameter: a positive integer N - number of elements in A.
Result: Random permutation of the A. array
SHUFFLE: procedure expose A.
parse arg N
do J = N to 2 by -1
R = RANDOM(1, J)
W = A.R; A.R = A.J; A.J = W
end
return
|
CONNECTIONS
Literature
Durstenfeld R. Random Permutation
CACM July 1964 No. 7, p. 420