PDF version (NAG web site
, 64-bit version, 64-bit version)
NAG Toolbox: nag_rand_permute (g05nc)
Purpose
nag_rand_permute (g05nc) performs a pseudorandom permutation of a vector of integers.
Syntax
Description
nag_rand_permute (g05nc) permutes the elements of an integer array without inspecting their values. Each of the possible permutations of the values may be regarded as being equally probable.
Even for modest values of it is theoretically impossible that all permutations may occur, as 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:
– int64int32nag_int array
-
The integer values to be permuted.
- 2:
– 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:
– int64int32nag_int scalar
-
Default:
the dimension of the array
indx.
The number of values to be permuted.
Constraint:
.
Output Parameters
- 1:
– int64int32nag_int array
-
The permuted integer values.
- 2:
– int64int32nag_int array
-
Contains updated information on the state of the generator.
- 3:
– int64int32nag_int scalar
unless the function detects an error (see
Error Indicators and Warnings).
Error Indicators and Warnings
Errors or warnings detected by the function:
-
-
Constraint: .
-
-
On entry,
state vector has been corrupted or not initialized.
-
An unexpected error has been triggered by this routine. Please
contact
NAG.
-
Your licence key may have expired or may not have been installed correctly.
-
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).
Open in the MATLAB editor:
g05nc_example
function g05nc_example
fprintf('g05nc example results\n\n');
seed = [int64(1762543)];
genid = int64(1);
subid = int64(1);
[state, ifail] = g05kf( ...
genid, subid, seed);
index = zeros(10, 8, 'int64');
index1 = int64([1:8]);
fprintf(' 10 Permutations of first 8 integers\n');
for j = 1:10
[index(j,:), state, ifail] = g05nc( ...
index1, state);
end
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)
© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015