HOWTO · MATLAB

How to Get Random Permutation Using MATLAB

Learn how MATLAB's randperm function creates full permutations and unique random samples, and how to reproduce a permutation by saving and restoring the random generator state.

On this page

MATLAB’s randperm function shuffles the integers in a range and can select a smaller set of unique indices for sampling data without replacement. This guide explains both forms and shows how saving and restoring the random generator state with rng reproduces a result.

Understand what randperm returns

randperm(n) returns a row vector containing each integer from 1 through n exactly once, in a randomly chosen order. For example, a call with n = 5 returns some ordering of 1, 2, 3, 4, and 5; the order can differ on a later call. Use the returned numbers as indices when you want to reorder or sample elements of an array.

The function returns integer indices, not a shuffled copy of your data. To shuffle data, index the data with the permutation. Because every index occurs once, this operation rearranges all entries without dropping or duplicating them.

Generate a full random permutation

Set the upper bound to the number of integers you want, then call randperm(n). This Octave example uses MATLAB-compatible syntax; the order is chosen at runtime, so no fixed result is shown.

Run this example:

n = 10;
permutation = randperm(n);
disp(permutation);

The result contains all integers from 1 through 10 once each. A different call can return a different order because the random generator state advances. For a data vector, data(randperm(numel(data))) gives a shuffled ordering of its elements.

Select items without replacement with randperm(n,k)

Use randperm(n,k) when you need k distinct indices drawn from 1 through n. The result is a row vector of length k, and k cannot exceed n, since there are only n distinct choices in the range. For k = n, this form selects the full range in random order.

To sample entries from a vector, use the selected indices directly. This example uses a sample size smaller than the data length:

data = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
sampleSize = 4;
indices = randperm(numel(data), sampleSize);
sample = data(indices);
disp(sample);

indices contains four distinct positions, and sample contains the corresponding values in that random order. Sampling this way is without replacement: an index cannot appear twice, although the data vector itself may contain equal values at different positions. Keep the indices if you also need to apply the same selection to aligned arrays, such as labels and observations.

Repeat a permutation by saving and restoring the random state with rng

A random result depends on the state of the generator. Save that state before the call, then restore it before repeating the same sequence of random operations. Restoring a saved state is what reproduces the sequence; two independent runs are not guaranteed to begin in the same state.

state = rng;
first = randperm(6);
rng(state);
second = randperm(6);
disp(isequal(first, second));

The final expression is true because the second call starts from the state used by the first call. If you want repeatability across separate runs, initialize the generator with an explicitly chosen seed using rng(seed) before generating the permutation. Reproducibility depends on restoring or initializing the random state and keeping the subsequent random operations in the same order.