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_repeat (g05kf)

## Purpose

nag_rand_init_repeat (g05kf) initializes the selected base generator, as used by the group of pseudorandom number functions (see nag_rand_init_leapfrog (g05kh)nag_rand_init_skipahead (g05kj), nag_rand_permute (g05nc), nag_rand_sample (g05nd), nag_rand_times_garch_asym1 (g05pd)nag_rand_times_mv_varma (g05pj), nag_rand_matrix_orthog (g05px)nag_rand_matrix_2waytable (g05pz), nag_rand_copula_students_t (g05rc), nag_rand_copula_normal (g05rd), nag_rand_multivar_students_t (g05ry), nag_rand_multivar_normal (g05rz) and nag_rand_dist_uniform01 (g05sa)nag_rand_int_uniform (g05tl)) and the quasi-random scrambled sequence initialization function, nag_rand_quasi_init_scrambled (g05yn).

## Syntax

[state, ifail] = g05kf(genid, subid, seed, 'lseed', lseed)
[state, ifail] = nag_rand_init_repeat(genid, subid, seed, 'lseed', lseed)

## Description

nag_rand_init_repeat (g05kf) selects a base generator through the input value of the parameters genid and subid, and then initializes it based on the values given in the array seed.
A given base generator will yield different sequences of random numbers if initialized with different values of seed. Alternatively, the same sequence of random numbers will be generated if the same value of seed is used. 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.

## References

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

## Parameters

### Compulsory Input Parameters

1:     genid – int64int32nag_int scalar
Must contain the type of base generator to use.
genid = 1${\mathbf{genid}}=1$
NAG basic generator.
genid = 2${\mathbf{genid}}=2$
Wichmann Hill I generator.
genid = 3${\mathbf{genid}}=3$
Mersenne Twister.
genid = 4${\mathbf{genid}}=4$
Wichmann Hill II generator.
genid = 5${\mathbf{genid}}=5$
ACORN generator.
genid = 6${\mathbf{genid}}=6$
L'Ecuyer MRG32k3a generator.
See the G05 Chapter Introduction for details of each of the base generators.
Constraint: genid = 1${\mathbf{genid}}=1$, 2$2$, 3$3$, 4$4$, 5$5$ or 6$6$.
2:     subid – int64int32nag_int scalar
If genid = 2${\mathbf{genid}}=2$, subid indicates which of the 273$273$ sub-generators to use. In this case, the ((|subid| + 272)  mod  273) + 1  sub-generator is used.
If genid = 5${\mathbf{genid}}=5$, subid indicates the values of k$k$ and p$p$ to use, where k$k$ is the order of the generator, and p$p$ controls the size of the modulus, M$M$, with M = 2(p × 30) $M={2}^{\left(p×30\right)}$. If subid < 1${\mathbf{subid}}<1$, the default values of k = 10$k=10$ and p = 2$p=2$ are used, otherwise values for k$k$ and p$p$ are calculated from the formula, subid = k + 1000(p1)${\mathbf{subid}}=k+1000\left(p-1\right)$.
If genid = 6${\mathbf{genid}}=6$ and subid  mod  2 = 0 the range of the generator is set to (0,1]$\left(0,1\right]$, otherwise the range is set to (0,1)$\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:     seed(lseed) – int64int32nag_int array
lseed, the dimension of the array, must satisfy the constraint lseed1${\mathbf{lseed}}\ge 1$.
The initial (seed) values for the selected base generator. The number of initial values required varies with each of the base generators.
If genid = 1${\mathbf{genid}}=1$, one seed is required.
If genid = 2${\mathbf{genid}}=2$, four seeds are required.
If genid = 3${\mathbf{genid}}=3$, 624$624$ seeds are required.
If genid = 4${\mathbf{genid}}=4$, four seeds are required.
If genid = 5${\mathbf{genid}}=5$, (k + 1)p$\left(k+1\right)p$ seeds are required, where k$k$ and p$p$ are defined by subid. For the ACORN generator it is recommended that an odd value is used for seed(1)${\mathbf{seed}}\left(1\right)$.
If genid = 6${\mathbf{genid}}=6$, six seeds are required.
If insufficient seeds are provided then the first lseed1${\mathbf{lseed}}-1$ values supplied in seed are used and the remaining values are randomly generated using the NAG basic generator. In such cases the NAG basic generator is initialized using the value supplied in ${\mathbf{seed}}\left({\mathbf{lseed}}\right)$.
Constraint: seed(i)1${\mathbf{seed}}\left(\mathit{i}\right)\ge 1$, for i = 1,2,,lseed$\mathit{i}=1,2,\dots ,{\mathbf{lseed}}$.

### Optional Input Parameters

1:     lseed – int64int32nag_int scalar
Default: The dimension of the array seed.
The size of the seed array.
Constraint: lseed1${\mathbf{lseed}}\ge 1$.

lstate

### Output Parameters

1:     state(lstate) – int64int32nag_int array
Contains information on the selected base generator and its current state.
2:     ifail – int64int32nag_int scalar
${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and Warnings]).
For environments where it might be inappropriate to halt program execution when an error is detected, the value 1​ or ​1$-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value 1$1$ is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is 0$0$. When the value 1​ or ​1$-\mathbf{1}\text{​ or ​}\mathbf{1}$ is used it is essential to test the value of ifail on exit.

## Error Indicators and Warnings

Errors or warnings detected by the function:
ifail = 1${\mathbf{ifail}}=-1$
Minimum length for state array returned in parameter lstate but state array not initialized.
ifail = 1${\mathbf{ifail}}=1$
On entry, genid1${\mathbf{genid}}\ne 1$, 2$2$, 3$3$, 4$4$, 5$5$ or 6$6$.
ifail = 2${\mathbf{ifail}}=2$
 On entry, invalid subid.
ifail = 3${\mathbf{ifail}}=3$
On entry, seed(i)0${\mathbf{seed}}\left(i\right)\le 0$ for at least one i$i$.
ifail = 4${\mathbf{ifail}}=4$
On entry, lseed < 1${\mathbf{lseed}}<1$.
ifail = 6${\mathbf{ifail}}=6$
On entry, lstate > 0$\mathit{lstate}>0$ and lstate is too small for specified base generator.

Not applicable.

None.

## Example

```function nag_rand_init_repeat_example
% Initialize the seed
seed = [int64(1762543)];
% genid and subid identify the base generator
genid = int64(1);
subid =  int64(1);
% The number of pseudorandom numbers to be generated
n = int64(5);
% Initialize the generator to a repeatable sequence
[state, ifail] = nag_rand_init_repeat(genid, subid, seed);
[state, x, ifail] = nag_rand_dist_uniform01(n, state)
```
```

state =

17
1234
1
0
4110
11820
23399
29340
17917
13895
19930
8
0
1234
1
1
1234

x =

0.6364
0.1065
0.7460
0.7983
0.1046

ifail =

0

```
```function g05kf_example
% Initialize the seed
seed = [int64(1762543)];
% genid and subid identify the base generator
genid = int64(1);
subid =  int64(1);
% The number of pseudorandom numbers to be generated
n = int64(5);
% Initialize the generator to a repeatable sequence
[state, ifail] = g05kf(genid, subid, seed);
[state, x, ifail] = g05sa(n, state)
```
```

state =

17
1234
1
0
4110
11820
23399
29340
17917
13895
19930
8
0
1234
1
1
1234

x =

0.6364
0.1065
0.7460
0.7983
0.1046

ifail =

0

```