﻿ g05kj Method
g05kj allows for the generation of multiple, independent, sequences of pseudorandom numbers using the skip-ahead method.
The base pseudorandom number sequence defined by state is advanced $n$ places.

# Syntax

C#
```public static void g05kj(
int n,
G05..::..G05State g05state,
out int ifail
)```
Visual Basic
```Public Shared Sub g05kj ( _
n As Integer, _
g05state As G05..::..G05State, _
<OutAttribute> ByRef ifail As Integer _
)```
Visual C++
```public:
static void g05kj(
int n,
G05..::..G05State^ g05state,
[OutAttribute] int% ifail
)```
F#
```static member g05kj :
n : int *
g05state : G05..::..G05State *
ifail : int byref -> unit
```

#### Parameters

n
Type: System..::..Int32
On entry: $n$, the number of places to skip ahead.
Constraint: ${\mathbf{n}}\ge 0$.
g05state
Type: NagLibrary..::..G05..::..G05State
An Object of type G05.G05State.
ifail
Type: System..::..Int32%
On exit: ${\mathbf{ifail}}={0}$ unless the method detects an error or a warning has been flagged (see [Error Indicators and Warnings]).

# Description

g05kj adjusts a base generator to allow multiple, independent, sequences of pseudorandom numbers to be generated via the skip-ahead method (see the G05 class for details).
If, prior to calling g05kj the base generator defined by state would produce random numbers ${x}_{1},{x}_{2},{x}_{3},\dots$, then after calling g05kj the generator will produce random numbers ${x}_{n+1},{x}_{n+2},{x}_{n+3},\dots$.
One of the initialization methods (G05KFF not in this release) (for a repeatable sequence if computed sequentially) or (G05KGF not in this release) (for a non-repeatable sequence) must be called prior to the first call to g05kj.
The skip-ahead algorithm can be used in conjunction with any of the six base generators discussed in G05 class.

# References

Haramoto H, Matsumoto M, Nishimura T, Panneton F and L'Ecuyer P (2008) Efficient jump ahead for F2-linear random number generators INFORMS J. on Computing 20(3) 385–390
Knuth D E (1981) The Art of Computer Programming (Volume 2) (2nd Edition) Addison–Wesley

# Error Indicators and Warnings

Errors or warnings detected by the method:
${\mathbf{ifail}}=1$
 On entry, ${\mathbf{n}}<0$.
${\mathbf{ifail}}=2$
 On entry, state vector was not initialized or has been corrupted.
${\mathbf{ifail}}=3$
On entry, cannot use the skip-ahead method with the base generator defined by state.
${\mathbf{ifail}}=4$
On entry, the base generator is Mersenne Twister, but the state vector defined on initialization is not large enough to perform a skip-ahead. See the initialization method (G05KFF not in this release) (G05KGF not in this release).
${\mathbf{ifail}}=-9000$
An error occured, see message report.
${\mathbf{ifail}}=-8000$
Negative dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$

Not applicable.

# Parallelism and Performance

None.

Calling g05kj and then generating a series of uniform values using g05sa is more efficient than, but equivalent to, calling g05sa and discarding the first $n$ values. This may not be the case for distributions other than the uniform, as some distributional generators require more than one uniform variate to generate a single draw from the required distribution.
To skip ahead $k×m$ places you can either
 (a) call g05kj once with ${\mathbf{n}}=k×m$, or (b) call g05kj $k$ times with ${\mathbf{n}}=m$, using the state vector output by the previous call as input to the next call
both approaches would result in the same sequence of values. When working in a multithreaded environment, where you want to generate (at most) $m$ values on each of $K$ threads, this would translate into either
 (a) spawning the $K$ threads and calling g05kj once on each thread with ${\mathbf{n}}=\left(k-1\right)×m$, where $k$ is a thread ID, taking a value between $1$ and $K$, or (b) calling g05kj on a single thread with ${\mathbf{n}}=m$, spawning the $K$ threads and then calling g05kj a further $k-1$ times on each of the thread.
Due to the way skip ahead is implemented for the Mersenne Twister, approach (a) will tend to be more efficient if more than 30 threads are being used (i.e., $K>30$), otherwise approach (b) should probably be used. For all other base generators, approach (a) should be used. See the G05 class for more details.