Main Content

Random permutation of integers

`p = randperm(`

returns a row vector
containing a random permutation of the integers from 1 to `n`

)`n`

without
repeating elements.

`p = randperm(`

generates a
random permutation of integers from random number stream `s`

,___)`s`

instead of
the default global stream. To create a stream, use `RandStream`

.
Specify `s`

followed by any of the argument combinations in previous
syntaxes.

The sequence of numbers produced by

`randperm`

is determined by the internal settings of the uniform pseudorandom number generator that underlies`rand`

,`randi`

,`randn`

, and`randperm`

. To control that shared random number generator, use the`rng`

function.The arrays returned by

`randperm`

contain permutation of integers without repeating integer values. This behavior is sometimes referred to as sampling without replacement. If you require repeating values, use the`randi`

function.`randperm(n)`

and`randperm(n,n)`

both generate permutations of the integers 1 through`n`

, but they can give different random orderings in the permutations. For large`n`

,`randperm(n,n)`

is faster than`randperm(n)`

.