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 x1 , x2 , x3 , ${x}_{1},{x}_{2},{x}_{3},\dots$, then after calling nag_rand_init_leapfrog (g05kh) the generator will produce random numbers xk , xk + n , xk + 2n , xk + 3n , ${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:     n – int64int32nag_int scalar
n$n$, the total number of sequences required.
Constraint: n > 0${\mathbf{n}}>0$.
2:     k – int64int32nag_int scalar
k$k$, the number of the current sequence.
Constraint: 0 < kn$0<{\mathbf{k}}\le {\mathbf{n}}$.
3:     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.

None.

None.

### Output Parameters

1:     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.
2:     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 = 1${\mathbf{ifail}}=1$
 On entry, n ≤ 0${\mathbf{n}}\le 0$.
ifail = 2${\mathbf{ifail}}=2$
 On entry, k > n${\mathbf{k}}>{\mathbf{n}}$.
ifail = 3${\mathbf{ifail}}=3$
 On entry, state vector was not initialized or has been corrupted.
ifail = 4${\mathbf{ifail}}=4$
On entry, cannot use the leap-frog method with the base generator defined by state.

## 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

```function nag_rand_init_leapfrog_example
% 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 = 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);

% Prepare n streams
for i=1:n
% Initialize the generator to a repeatable sequence
[state(:, i), ifail] = nag_rand_init_repeat(genid, subid, seed);
% Prepare the i'th out of n streams
[state(:, i), ifail] = nag_rand_init_leapfrog(int64(n), int64(i), state(:,i));
end
% Generate nv variates from a uniform distribution, from each stream
for i=1:n
fprintf('\n Stream %d\n', i);
[state(:, i), x(:, i), ifail] = nag_rand_dist_uniform01(nv, state(:, i));
fprintf('%10.4f\n', x(:, i));
end
```
```

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

```
```function g05kh_example
% 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 = 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);

% Prepare n streams
for i=1:n
% Initialize the generator to a repeatable sequence
[state(:, i), ifail] = g05kf(genid, subid, seed);
% Prepare the i'th out of n streams
[state(:, i), ifail] = g05kh(int64(n), int64(i), state(:,i));
end
% Generate nv variates from a uniform distribution, from each stream
for i=1:n
fprintf('\n Stream %d\n', i);
[state(:, i), x(:, i), ifail] = g05sa(nv, state(:, i));
fprintf('%10.4f\n', x(:, i));
end
```
```

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

```