Language Reference


RANPERM Function

RANPERM (n); RANPERM (set, <, numperm> );

The RANPERM function generates random permutations of a set with n elements. The random number seed is set by the RANDSEED subroutine .

The first argument, set, can be a scalar or a vector. If set is a scalar, the function returns indices in the range 1–n. If set is a vector, the number of elements of the vector determines n and the RANPERM function returns elements of set, which can be numeric or character.

By default, the RANPERM function returns a single random combination with one row and n columns. If the numperm argument is specified, the function returns a matrix with numperm rows and n columns. Each row of the returned matrix represents a single permutation.

The following statements generate five random permutations of the set $\{ 1, 2, 3\} $:

call randseed(1234);
n = 3;
p = ranperm(n, 5);
print p;

Figure 24.308: Random Permutations of Three Items

p
1 2 3
3 2 1
3 2 1
3 1 2
3 1 2



Alternatively, the following statements compute five random permutations of an unsorted character vector:

a = ranperm({C B A}, 5);
print a;

Figure 24.309: Random Permutations of a Character Vector

a
B C A
B A C
C B A
B C A
B C A