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


Cover Contents Index Main page Rexx page   Mail

last modified 6th August 2001
Copyright © 2000-2001 Vladimir Zabrodsky
Czech Republic
 

1