Random permutation
p = randperm(n)
p = randperm(n,k)
p = randperm(n)
returns a row vector containing a random permutation of the integers from 1 ton
inclusive.
p = randperm(n,k)
returns a row vector containingk
unique integers selected randomly from 1 ton
inclusive.
randperm(6)
[3 2 6 4 1 5]
randperm
would in most cases return two different vectors:randperm(6) ans = 5 2 6 4 1 3 randperm(6) ans = 4 1 6 2 3 5
randperm (6,3)
[4 2 5]
Forp = randperm(n,k)
,p
containsk
uniquevalues.randperm
performs k-permutations (sampling without replacement). To allow repeated values in the output (sampling with replacement), userandi(n,1,k)
.
randperm
uses the same random number generator asrand
,randi
, andrandn
. You control this generator withrng
.