R Language Random permutations


Example

To generate random permutation of 5 numbers:

sample(5)
# [1] 4 5 3 1 2

To generate random permutation of any vector:

sample(10:15)
# [1] 11 15 12 10 14 13

One could also use the package pracma

randperm(a, k)
# Generates one random permutation of k of the elements a, if a is a vector,
# or of 1:a if a is a single integer.
# a: integer or numeric vector of some length n.
# k: integer, smaller as a or length(a).

# Examples
library(pracma)
randperm(1:10, 3)
[1] 3 7 9

randperm(10, 10)
[1]  4  5 10  8  2  7  6  9  3  1

randperm(seq(2, 10, by=2))
[1]  6  4 10  2  8