# NAG Library Routine Document

## 1Purpose

g05kgf initializes the selected base generator to generate a non-repeatable sequence of variates. The base generator can then be used by the group of pseudorandom number routines (see g05khfg05kjf, g05ncf, g05ndf, g05pdfg05pjf, g05pxfg05pzf, g05rcf, g05rdf, g05ryf, g05rzf and g05safg05tlf) and the quasi-random scrambled sequence initialization routine, g05ynf.

## 2Specification

Fortran Interface
 Subroutine g05kgf (
 Integer, Intent (In) :: genid, subid Integer, Intent (Inout) :: state(lstate), lstate, ifail
#include <nagmk26.h>
 void g05kgf_ (const Integer *genid, const Integer *subid, Integer state[], Integer *lstate, Integer *ifail)

## 3Description

g05kgf selects a base generator through the input value of the arguments genid and subid, and then initializes it based on the values taken from the real-time clock, resulting in the same base generator yielding different sequences of random numbers each time the calling program is run. It should be noted that there is no guarantee of statistical properties between sequences, only within sequences.
A definition of some of the terms used in this description, along with details of the various base generators can be found in the G05 Chapter Introduction.

## 4References

L'Ecuyer P and Simard R (2002) TestU01: a software library in ANSI C for empirical testing of random number generators Departement d'Informatique et de Recherche Operationnelle, Universite de Montreal http://www.iro.umontreal.ca/~lecuyer
Maclaren N M (1989) The generation of multiple independent sequences of pseudorandom numbers Appl. Statist. 38 351–359
Matsumoto M and Nishimura T (1998) Mersenne twister: a 623-dimensionally equidistributed uniform pseudorandom number generator ACM Transactions on Modelling and Computer Simulations
Wichmann B A and Hill I D (2006) Generating good pseudo-random numbers Computational Statistics and Data Analysis 51 1614–1622
Wikramaratna R S (1989) ACORN - a new method for generating sequences of uniformly distributed pseudo-random numbers Journal of Computational Physics 83 16–31

## 5Arguments

1:     $\mathbf{genid}$ – IntegerInput
On entry: must contain the type of base generator to use.
${\mathbf{genid}}=1$
NAG basic generator.
${\mathbf{genid}}=2$
Wichmann Hill I generator.
${\mathbf{genid}}=3$
Mersenne Twister.
${\mathbf{genid}}=4$
Wichmann Hill II generator.
${\mathbf{genid}}=5$
ACORN generator.
${\mathbf{genid}}=6$
L'Ecuyer MRG32k3a generator.
See the G05 Chapter Introduction for details of each of the base generators.
Constraint: ${\mathbf{genid}}=1$, $2$, $3$, $4$, $5$ or $6$.
2:     $\mathbf{subid}$ – IntegerInput
On entry: if ${\mathbf{genid}}=2$, subid indicates which of the $273$ sub-generators to use. In this case, the  sub-generator is used.
If ${\mathbf{genid}}=5$, subid indicates the values of $k$ and $p$ to use, where $k$ is the order of the generator, and $p$ controls the size of the modulus, $M$, with $M={2}^{\left(p×30\right)}$. If ${\mathbf{subid}}<1$, the default values of $k=10$ and $p=2$ are used, otherwise values for $k$ and $p$ are calculated from the formula, ${\mathbf{subid}}=k+1000\left(p-1\right)$.
If ${\mathbf{genid}}=6$ and  the range of the generator is set to $\left(0,1\right]$, otherwise the range is set to $\left(0,1\right)$; in this case the sequence is identical to the implementation of MRG32k3a in TestU01 (see L'Ecuyer and Simard (2002)) for identical seeds.
For all other values of genid, subid is not referenced.
3:     $\mathbf{state}\left({\mathbf{lstate}}\right)$ – Integer arrayCommunication Array
On exit: contains information on the selected base generator and its current state.
4:     $\mathbf{lstate}$ – IntegerInput/Output
On entry: the dimension of the state array, or a value $<1$. If the Mersenne Twister (${\mathbf{genid}}=3$) is being used and the skip ahead routine g05kjf or g05kkf will be called subsequently, then you must ensure that ${\mathbf{lstate}}\ge 1260$.
On exit: if ${\mathbf{lstate}}<1$ on entry, then the required length of the state array for the chosen base generator, otherwise lstate is unchanged. When ${\mathbf{genid}}=3$ (Mersenne Twister) a value of $1260$ is returned, allowing for the skip ahead routine to be subsequently called. In all other cases the minimum length, as documented in the constraints below, is returned.
Constraints:
• if ${\mathbf{genid}}=1$, ${\mathbf{lstate}}\ge 17$;
• if ${\mathbf{genid}}=2$, ${\mathbf{lstate}}\ge 21$;
• if ${\mathbf{genid}}=3$, ${\mathbf{lstate}}\ge 633$;
• if ${\mathbf{genid}}=4$, ${\mathbf{lstate}}\ge 29$;
• if ${\mathbf{genid}}=5$, ${\mathbf{lstate}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(\left(k+1\right)×p+9,14\right)+3$, where $k$ and $p$ are defined by subid;
• if ${\mathbf{genid}}=6$, ${\mathbf{lstate}}\ge 61$;
• otherwise ${\mathbf{lstate}}<1$.
5:     $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, . If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation for details.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ or $-1$ unless the routine detects an error or a warning has been flagged (see Section 6).
For environments where it might be inappropriate to halt program execution when an error is detected, the value  is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this argument, the recommended value is $0$. When the value  is used it is essential to test the value of ifail on exit.

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-1$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
${\mathbf{ifail}}=1$
On entry, ${\mathbf{genid}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{genid}}=1$, $2$, $3$, $4$, $5$ or $6$.
${\mathbf{ifail}}=4$
On entry, ${\mathbf{lstate}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{lstate}}\le 0$ or ${\mathbf{lstate}}\ge 〈\mathit{\text{value}}〉$.
${\mathbf{ifail}}=-1$
Required length of state array returned in lstate but state array not initialized.
${\mathbf{ifail}}=-99$
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

Not applicable.

## 8Parallelism and Performance

g05kgf is not threaded in any implementation.

None.

## 10Example

In order to preserve the statistical properties of the base generators, g05kgf should only be called once. If multiple streams of values are required then one of the methods described in Section 2.1.1 in the G05 Chapter Introduction should be used.
However, for illustrative purposes only, this example calls g05kgf twice. At each call a sample of $500$ values from a discrete uniform distribution are generated and then the two samples are compared.

### 10.1Program Text

Program Text (g05kgfe.f90)

None.

### 10.3Program Results

Program Results (g05kgfe.r)