randperm

..  randperm([rng,] n)

Construct a random permutation of length ``n``. The optional ``rng`` argument
specifies a random number generator, see :ref:`Random Numbers <random-numbers>`.

Examples

In the Julia programming language, the function randperm(n) constructs a random permutation of length n. Optionally, you can provide an additional rng argument to specify a random number generator.

julia> randperm(5)
5-element Array{Int64,1}:
 3
 2
 4
 1
 5

This example generates a random permutation of length 5.

julia> rng = MersenneTwister(123);  # Create a specific random number generator
julia> randperm(rng, 10)
10-element Array{Int64,1}:
 8
 9
 6
 4
 2
 1
 3
 7
 5
10

Here, we use a specific random number generator MersenneTwister(123) to generate a random permutation of length 10.

Common mistake example:

julia> randperm(-3)
ERROR: ArgumentError: n must be nonnegative

In this example, the function call randperm(-3) throws an error because the length n provided is negative. Make sure to provide a non-negative value for the length n to avoid this error.

See Also

bitrand, MersenneTwister, rand, randcycle, randexp, randexp!, randjump, randn, randn!, RandomDevice, randperm, randsubseq, randsubseq!, shuffle, srand,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: