Random permutation of integers
collapse all in page
Syntax
p = randperm(n)
p = randperm(n,k)
p = randperm(s,___)
Description
example
p = randperm(n)
returns a row vector containing a random permutation of the integers from 1 to n
without repeating elements.
example
p = randperm(n,k)
returns a row vector containing k
unique integers selected randomly from 1 to n
.
p = randperm(s,___)
generates a random permutation of integers from random number stream 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.
Examples
collapse all
Random Permutation of Integers
Open Live Script
Generate a random permutation of the integers from 1 to 6. The input to randperm
indicates the largest integer in the sampling interval (the smallest integer in the interval is 1).
r = randperm(6)
r = 1×6 6 3 5 1 2 4
Permute a Selection of Integers
Open Live Script
Generate a random permutation of four unique integers (without repeating elements) selected randomly from the integers 1 to 8.
r1 = randperm(8,4)
r1 = 1×4 6 4 7 3
Generate another random permutation of four unique integers.
r2 = randperm(8,4)
r2 = 1×4 8 7 5 4
Control Random Number Generation
Open Live Script
Save the current state of the random number generator and create a random permutation of the integers from 1 to 8.
s = rng;r = randperm(8)
r = 1×8 6 3 7 8 5 1 2 4
Restore the state of the random number generator to s
, and then create a new random permutation of the integers from 1 to 8. The permutation is the same as before.
rng(s)r1 = randperm(8)
r1 = 1×8 6 3 7 8 5 1 2 4
You can use the rng function to specify the settings of the random number generator.
Input Arguments
collapse all
n
— Number of integers in sample interval
positive integer
Number of integers in sample interval, specified as a positive integer. randperm
permutes integer values from 1 to n
inclusive.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
k
— Number of selected integers
positive integer
Number of selected integers, specified as a positive integer. k
must also be less than or equal to n
.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
s
— Random number stream
RandStream
object
Random number stream, specified as a RandStream object.
Example: s = RandStream('dsfmt19937'); randperm(s,10)
Tips
The sequence of numbers produced by
randperm
is determined by the internal settings of the uniform pseudorandom number generator that underliesrand
,randi
,randn
, andrandperm
. 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)
andrandperm(n,n)
both generate permutations of the integers 1 throughn
, but they can give different random orderings in the permutations. For largen
,randperm(n,n)
is faster thanrandperm(n)
.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
Usage notes and limitations:
To run this function on a GPU, use the syntax
p = gpuArray.randperm(n)
.The two-input syntax is not supported.
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced before R2006a
See Also
permute | nchoosek | randi | perms | rng
Topics
- Create Arrays of Random Numbers
- Generate Random Numbers That Are Repeatable
- Generate Random Numbers That Are Different
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本 (日本語)
- 한국 (한국어)
Contact your local office