randomPermutation

Generates a pseudorandom permutation.

Synopsis

randomPermutation (k)

Required Arguments

int k (Input)
Number of integers to be permuted.

Return Value

An array of length k containing the random permutation of the integers from 1 to k.

Description

Function randomPermutation generates a pseudorandom permutation of the integers from 1 to k. It begins by filling a vector of length k with the consecutive integers 1 to k. Then, with M initially equal to k, a random index J between 1 and M (inclusive) is generated. The element of the vector with the index M and the element with index J swap places in the vector. M is then decremented by 1 and the process repeated until \(M=1\).

Example

In this example, randomPermutation is called to produce a pseudorandom permutation of the integers from 1 to 10.

from __future__ import print_function
from numpy import *
from pyimsl.stat.randomPermutation import randomPermutation
from pyimsl.stat.randomSeedSet import randomSeedSet
from pyimsl.stat.writeMatrix import writeMatrix

k = 10
randomSeedSet(123457)
ir = randomPermutation(k)
print("Random permutation of the integers from 1 to 10")
writeMatrix("", ir, writeFormat="%3i", noColLabels=True)

Output

Random permutation of the integers from 1 to 10
 
  5    9    2    8    1    6    4    7    3   10