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_init_leapfrog (g05kh)

## Purpose

nag_rand_init_leapfrog (g05kh) allows for the generation of multiple, independent, sequences of pseudorandom numbers using the leap-frog method.

## Syntax

[state, ifail] = g05kh(n, k, state)
[state, ifail] = nag_rand_init_leapfrog(n, k, state)

## Description

nag_rand_init_leapfrog (g05kh) adjusts a base generator to allow multiple, independent, sequences of pseudorandom numbers to be generated via the leap-frog method (see the G05 Chapter Introduction for details).
If, prior to calling nag_rand_init_leapfrog (g05kh) the base generator defined by state would produce random numbers ${x}_{1},{x}_{2},{x}_{3},\dots$, then after calling nag_rand_init_leapfrog (g05kh) the generator will produce random numbers ${x}_{k},{x}_{k+n},{x}_{k+2n},{x}_{k+3n},\dots$.
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_init_leapfrog (g05kh).
The leap-frog algorithm can be used in conjunction with the NAG basic generator, both the Wichmann–Hill I and Wichmann–Hill II generators, the Mersenne Twister and L'Ecuyer.

## References

Knuth D E (1981) The Art of Computer Programming (Volume 2) (2nd Edition) Addison–Wesley

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{n}$int64int32nag_int scalar
$n$, the total number of sequences required.
Constraint: ${\mathbf{n}}>0$.
2:     $\mathrm{k}$int64int32nag_int scalar
$k$, the number of the current sequence.
Constraint: $0<{\mathbf{k}}\le {\mathbf{n}}$.
3:     $\mathrm{state}\left(:\right)$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.

None.

### Output Parameters

1:     $\mathrm{state}\left(:\right)$int64int32nag_int array
Contains updated information on the state of the generator.
2:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{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:
${\mathbf{ifail}}=1$
Constraint: ${\mathbf{n}}\ge 1$.
${\mathbf{ifail}}=2$
Constraint: $0<{\mathbf{k}}\le {\mathbf{n}}$.
${\mathbf{ifail}}=3$
On entry, state vector has been corrupted or not initialized.
${\mathbf{ifail}}=4$
On entry, cannot use leap-frog with the base generator defined by state.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## Accuracy

Not applicable.

The leap-frog method tends to be less efficient than other methods of producing multiple, independent sequences. See the G05 Chapter Introduction for alternative choices.

## Example

This example creates three independent sequences using nag_rand_init_leapfrog (g05kh), after initialization by nag_rand_init_repeat (g05kf). Five variates from a uniform distribution are then generated from each sequence using nag_rand_dist_uniform01 (g05sa).
```function g05kh_example

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

% Initialize the seed
seed = [int64(1762543)];

% genid and subid identify the base generator
genid = int64(1);
subid =  int64(1);
lstate = int64(17);
lseed =  int64(1);

% n is the number of streams
n = int64(3);
% nv is the number of variates
nv = int64(5);
% Hold state and variates in successive columns
state = zeros(lstate, n, 'int64');
x = zeros(nv, n);

% Initialize the generator to a repeatable sequence for streams
[state1, ifail] = g05kf( ...
genid, subid, seed);
% Prepare n streams
for i=1:n
% Prepare stream i
state(:, i) = state1;
[state(:, i), ifail] = g05kh( ...
n, i, state(:,i));
end

% Generate nv variates from a uniform distribution, from each stream
for i=1:n
[state(:, i), x(:, i), ifail] = g05sa( ...
nv, state(:, i));
end

% Display variates by stream
for i=1:n
fprintf(' Stream %d\n', i);
disp(x(:,i));
end

```
```g05kh example results

Stream 1
0.7460
0.4925
0.4982
0.2580
0.5938

Stream 2
0.7983
0.3843
0.6717
0.6238
0.2785

Stream 3
0.1046
0.7871
0.0505
0.0535
0.2375

```