randperm (RandStream)

Random permutation

Syntax

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

Description

p = randperm(s,n) returns a row vector containing a random permutation of integers from 1 to n inclusive. randperm(s,n) uses random values drawn from the random stream s.

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

Examples

Create a random stream s and generate a random permutation of the integers from 1 to 6 based on s:

s = RandStream('mt19937ar','Seed',0);
randperm(s,6)

MATLAB® returns the vector

[6     3     5     1     2     4]

Use the random stream s to generate three integers between 1 and 10:

randperm(s,10,3)
ans =
      1     8     9

More About

expand all

Tips

For p = randperm(s,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(s,n,1,k).

Was this topic helpful?