hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_rand_permute (g05nc)

 Contents

    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example

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! possible permutations of the n values may be regarded as being equally probable.
Even for modest values of n it is theoretically impossible that all n! permutations may occur, as 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:     indxn int64int32nag_int array
The 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.

Output Parameters

1:     indxn int64int32nag_int array
The n permuted integer values.
2:     state: int64int32nag_int array
Contains updated information on the state of the generator.
3:     ifail int64int32nag_int scalar
ifail=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
Constraint: n1.
   ifail=3
On entry, state vector has been corrupted or not initialized.
   ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
   ifail=-399
Your licence key may have expired or may not have been installed correctly.
   ifail=-999
Dynamic memory allocation failed.

Accuracy

Not applicable.

Further Comments

None.

Example

In the example program a vector containing the first eight positive integers in ascending order is permuted by a call to nag_rand_permute (g05nc) and the permutation is printed. This is repeated a total of ten times, after initialization by nag_rand_init_repeat (g05kf).
function g05nc_example


fprintf('g05nc example results\n\n');

% Initialize the seed and identify the base generator
seed  = [int64(1762543)];
genid = int64(1);
subid =  int64(1);

% Initialize the generator to a repeatable sequence
[state, ifail] = g05kf( ...
                        genid, subid, seed);

index = zeros(10, 8, 'int64');

% Set up the index vector
index1 = int64([1:8]);

fprintf(' 10 Permutations of first 8 integers\n');
% Permutate 10 times
for j = 1:10
  % Call the permutation routine
  [index(j,:), state, ifail] = g05nc( ...
                                      index1, state);
end

% Display variates
disp(double(index));


g05nc example results

 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


PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015