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

 Contents

    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example

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 , , then after calling nag_rand_init_leapfrog (g05kh) the generator will produce random numbers xk , xk+n , xk+2n , xk+3n , .
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, the total number of sequences required.
Constraint: n>0.
2:     k int64int32nag_int scalar
k, the number of the current sequence.
Constraint: 0<kn.
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.

Optional Input Parameters

None.

Output Parameters

1:     state: int64int32nag_int array
Contains updated information on the state of the generator.
2:     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=1
Constraint: n1.
   ifail=2
Constraint: 0<kn.
   ifail=3
On entry, state vector has been corrupted or not initialized.
   ifail=4
On entry, cannot use leap-frog with the base generator defined by state.
   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

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


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