Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_rand_permute (g05nc)

Purpose

nag_rand_permute (g05nc) performs a pseudorandom permutation of a vector of integers.

Syntax

[indx, state, ifail] = g05nc(indx, state, 'n', n)
[indx, state, ifail] = nag_rand_permute(indx, state, 'n', n)

Description

nag_rand_permute (g05nc) permutes the elements of an integer array without inspecting their values. Each of the n ! $n!$ possible permutations of the n$n$ values may be regarded as being equally probable.
Even for modest values of n$n$ it is theoretically impossible that all n ! $n!$ permutations may occur, as n ! $n!$ is likely to exceed the cycle length of any of the base generators. For practical purposes this is irrelevant, as the time necessary to generate all possible permutations is many millenia.
One of the initialization functions nag_rand_init_repeat (g05kf) (for a repeatable sequence if computed sequentially) or nag_rand_init_nonrepeat (g05kg) (for a non-repeatable sequence) must be called prior to the first call to nag_rand_permute (g05nc).

References

Kendall M G and Stuart A (1969) The Advanced Theory of Statistics (Volume 1) (3rd Edition) Griffin
Knuth D E (1981) The Art of Computer Programming (Volume 2) (2nd Edition) Addison–Wesley

Parameters

Compulsory Input Parameters

1:     indx(n) – int64int32nag_int array
n, the dimension of the array, must satisfy the constraint n1${\mathbf{n}}\ge 1$.
The n$n$ integer values to be permuted.
2:     state( : $:$) – int64int32nag_int array
Note: the actual argument supplied must be the array state supplied to the initialization routines nag_rand_init_repeat (g05kf) or nag_rand_init_nonrepeat (g05kg).
Contains information on the selected base generator and its current state.

Optional Input Parameters

1:     n – int64int32nag_int scalar
Default: The dimension of the array indx.
The number of values to be permuted.
Constraint: n1${\mathbf{n}}\ge 1$.

None.

Output Parameters

1:     indx(n) – int64int32nag_int array
The n$n$ permuted integer values.
2:     state( : $:$) – int64int32nag_int array
Note: the actual argument supplied must be the array state supplied to the initialization routines nag_rand_init_repeat (g05kf) or nag_rand_init_nonrepeat (g05kg).
Contains updated information on the state of the generator.
3:     ifail – int64int32nag_int scalar
${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and Warnings]).

Error Indicators and Warnings

Errors or warnings detected by the function:
ifail = 2${\mathbf{ifail}}=2$
On entry, n < 1${\mathbf{n}}<1$.
ifail = 3${\mathbf{ifail}}=3$
 On entry, state vector was not initialized or has been corrupted.

Not applicable.

None.

Example

```function nag_rand_permute_example
% Initialize the seed
seed = [int64(1762543)];
% genid and subid identify the base generator
genid = int64(1);
subid =  int64(1);
% Initialize the generator to a repeatable sequence
[state, ifail] = nag_rand_init_repeat(genid, subid, seed);
indexOut = zeros(10, 8, 'int64');
ifail = zeros(10, 1, 'int64');
fprintf('\n 10 Permutations of first 8 integers\n');
% Permutate 10 times
for j = 1:10
% Set up the index vector
index = [int64(1);2;3;4;5;6;7;8];
% Call the permutation routine
[indexOut(j,:), state, ifail(j)] = nag_rand_permute(index, state);
if (ifail == 0)
% Display the results
fprintf(' %d ', indexOut(j,:));
fprintf('\n');
end
end
```
```

10 Permutations of first 8 integers
6  2  4  8  1  3  5  7
8  6  4  2  7  3  1  5
4  2  8  7  5  6  3  1
1  6  4  5  2  3  7  8
1  7  3  8  4  2  5  6
6  3  4  7  1  2  8  5
6  4  1  8  2  5  3  7
3  2  1  7  5  8  6  4
4  2  1  5  3  6  8  7
1  5  6  4  2  7  8  3

```
```function g05nc_example
% Initialize the seed
seed = [int64(1762543)];
% genid and subid identify the base generator
genid = int64(1);
subid =  int64(1);
% Initialize the generator to a repeatable sequence
[state, ifail] = g05kf(genid, subid, seed);
indexOut = zeros(10, 8, 'int64');
ifail = zeros(10, 1, 'int64');
fprintf('\n 10 Permutations of first 8 integers\n');
% Permutate 10 times
for j = 1:10
% Set up the index vector
index = [int64(1);2;3;4;5;6;7;8];
% Call the permutation routine
[indexOut(j,:), state, ifail(j)] = g05nc(index, state);
if (ifail == 0)
% Display the results
fprintf(' %d ', indexOut(j,:));
fprintf('\n');
end
end
```
```

10 Permutations of first 8 integers
6  2  4  8  1  3  5  7
8  6  4  2  7  3  1  5
4  2  8  7  5  6  3  1
1  6  4  5  2  3  7  8
1  7  3  8  4  2  5  6
6  3  4  7  1  2  8  5
6  4  1  8  2  5  3  7
3  2  1  7  5  8  6  4
4  2  1  5  3  6  8  7
1  5  6  4  2  7  8  3

```