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 (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

collapse 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?