Language Reference


RANPERK Function

RANPERK (n, k <, numperm> );

RANPERK (set, k <, numperm> );

The RANPERK function generates a random permutation of k elements from a set of n elements. The random number seed is set by the RANDSEED subroutine. The RANPERK function is similar to the RANCOMB function. A combination is a sorted permutation of the k elements.

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

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

The following statements generate four random permutations that consist of two elements from the set 1, 2, 3:

call randseed(1234);
n = 3;
p = ranperk(n, 2, 4);
print p;

Figure 25.309: Two Elements of a Random Permutation

p
3 1
1 2
3 2
1 3



Alternatively, the following statements compute random permutations that consist of two elements from an unsorted character vector:

q = ranperk({C B A}, 2, 4);
print q;

Figure 25.310: Random Permutation of a Character Vector

q
C A
C A
B C
A C