randperm (RandStream)

Random permutation


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


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.


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);
MATLAB® returns the vector
[6     3     5     1     2     4]

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

ans =
      1     8     9


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).

Extended Capabilities