random_permutation

Generates a pseudorandom permutation.

Synopsis

#include <imsls.h>

int *imsls_random_permutation (int k, ..., 0)

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. To release this space, use imsls_free.

Synopsis with Optional Arguments

#include <imsls.h>

int *imsls_random_permutation (int k,

IMSLS_RETURN_USER, int ir[],

0)

Optional Arguments

IMSLS_RETURN_USER, int ir[] (Output)
User-supplied array of length k containing the random permutation of the integers from 1 to k.

Description

Function imsls_random_permutation 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, imsls_random_permutation is called to produce a pseudorandom permutation of the integers from 1 to 10.

 

#include <stdio.h>

#include <imsls.h>

 

int main()

{

int *ir, k = 10;

 

imsls_random_seed_set(123457);

 

ir = imsls_random_permutation(k, 0);

 

printf("Random permutation of the integers from 1 to 10\n");

imsls_i_write_matrix("", 1, k, ir,

IMSLS_NO_COL_LABELS, 0);

}

Output

 

Random permutation of the integers from 1 to 10

 

5 9 2 8 1 6 4 7 3 10