Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

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 to n inclusive.

p = randperm(n,k) returns a row vector containing k unique integers selected randomly from 1 to n inclusive.

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 to randperm would 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.

More About

collapse all

Tips

For p = randperm(n,k), p contains k unique values. randperm performs k-permutations (sampling without replacement). To allow repeated values in the output (sampling with replacement), use randi(n,1,k).

randperm uses the same random number generator as rand, randi, and randn. You control this generator with rng.

Introduced before R2006a

Was this topic helpful?