Skip to Main Content Skip to Search
Product 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 to n inclusive.

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

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.

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.

See Also

nchoosek | perms | permute | randi | randperm(RandStream) | rng

  


» Learn more
» Download free kit
» Get trial software

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS