Documentation

randperm

Random permutation

Syntax

p = randperm(n)
p = randperm(n,k)

Description

p = randperm(n)returns a row vector containing a random permutation of the integers from 1 toninclusive.

p = randperm(n,k)returns a row vector containingkunique integers selected randomly from 1 toninclusive.

Examples

randperm(6)
might be the vector
[3 2 6 4 1 5]
or it might be some other permutation of the integers from 1 to 6, depending on the state of the random number generator. Two successive calls torandpermwould in most cases return two different vectors:
randperm(6) ans = 5 2 6 4 1 3 randperm(6) ans = 4 1 6 2 3 5

randperm (6,3)
might be the vector
[4 2 5]
or it might be some other permutation of any three integers from 1 to 6 inclusive, depending on the state of the random number generator.

Tips

Forp = randperm(n,k),pcontainskuniquevalues.randpermperforms k-permutations (sampling without replacement). To allow repeated values in the output (sampling with replacement), userandi(n,1,k).

randpermuses the same random number generator asrand,randi, andrandn. You control this generator withrng.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Introduced before R2006a

Was this topic helpful?