g05tg generates a sequence of $n$ variates, each consisting of $k$ pseudorandom integers, from the discrete multinomial distribution with $k$ outcomes and $m$ trials, where the outcomes have probabilities ${p}_{1},{p}_{2},\dots ,{p}_{k}$ respectively.

# Syntax

C#
```public static void g05tg(
int mode,
int n,
int m,
int k,
double[] p,
double[] r,
G05..::..G05State g05state,
int[,] x,
out int ifail
)```
Visual Basic
```Public Shared Sub g05tg ( _
mode As Integer, _
n As Integer, _
m As Integer, _
k As Integer, _
p As Double(), _
r As Double(), _
g05state As G05..::..G05State, _
x As Integer(,), _
<OutAttribute> ByRef ifail As Integer _
)```
Visual C++
```public:
static void g05tg(
int mode,
int n,
int m,
int k,
array<double>^ p,
array<double>^ r,
G05..::..G05State^ g05state,
array<int,2>^ x,
[OutAttribute] int% ifail
)```
F#
```static member g05tg :
mode : int *
n : int *
m : int *
k : int *
p : float[] *
r : float[] *
g05state : G05..::..G05State *
x : int[,] *
ifail : int byref -> unit
```

#### Parameters

mode
Type: System..::..Int32
On entry: a code for selecting the operation to be performed by the method.
${\mathbf{mode}}=0$
Set up reference vector only.
${\mathbf{mode}}=1$
Generate variates using reference vector set up in a prior call to g05tg.
${\mathbf{mode}}=2$
Set up reference vector and generate variates.
${\mathbf{mode}}=3$
Generate variates without using the reference vector.
Constraint: ${\mathbf{mode}}=0$, $1$, $2$ or $3$.
n
Type: System..::..Int32
On entry: $n$, the number of pseudorandom numbers to be generated.
Constraint: ${\mathbf{n}}\ge 0$.
m
Type: System..::..Int32
On entry: $m$, the number of trials of the multinomial distribution.
Constraint: ${\mathbf{m}}\ge 0$.
k
Type: System..::..Int32
On entry: $k$, the number of possible outcomes of the multinomial distribution.
Constraint: ${\mathbf{k}}\ge 2$.
p
Type: array<System..::..Double>[]()[][]
An array of size [k]
On entry: contains the probabilities ${p}_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,k$, of the $k$ possible outcomes of the multinomial distribution.
Constraint: $0.0\le {\mathbf{p}}\left[j-1\right]\le 1.0$ and $\sum _{j=1}^{k}{\mathbf{p}}\left[j-1\right]=1.0$.
r
Type: array<System..::..Double>[]()[][]
An array of size [lr]
On entry: if ${\mathbf{mode}}=1$, the reference vector from the previous call to g05tg.
If ${\mathbf{mode}}=3$, r is not referenced.
On exit: if ${\mathbf{mode}}\ne 3$, the reference vector.
g05state
Type: NagLibrary..::..G05..::..G05State
An Object of type G05.G05State.
x
Type: array<System..::..Int32,2>[,](,)[,][,]
An array of size [dim1, _tdx]
Note: dim1 must satisfy the constraint: $\mathrm{dim1}\ge {\mathbf{n}}$
On exit: the first $n$ rows of ${\mathbf{x}}\left[i-1,j-1\right]$ each contain $k$ pseudorandom numbers representing a $k$-dimensional variate from the specified multinomial distribution.
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

g05tg generates a sequence of $n$ groups of $k$ integers ${x}_{\mathit{i},\mathit{j}}$, for $\mathit{j}=1,2,\dots ,k$ and $\mathit{i}=1,2,\dots ,n$, from a multinomial distribution with $m$ trials and $k$ outcomes, where the probability of ${x}_{\mathit{i},\mathit{j}}={I}_{j}$ for each $j=1,2,\dots ,k$ is
 $Pi1=I1,…,ik=Ik=m!∏j=1kIj!∏j=1kpjIj=m!I1!I2!⋯Ik!p1I1p2I2⋯pkIk,$
where
 $∑j=1kpj=1 and ∑j=1kIj=m.$
A single trial can have several outcomes ($k$) and the probability of achieving each outcome is known (${p}_{j}$). After $m$ trials each outcome will have occurred a certain number of times. The $k$ numbers representing the numbers of occurrences for each outcome after $m$ trials is then a single sample from the multinomial distribution defined by the parameters $k$, $m$ and ${p}_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,k$. This method returns $n$ such samples.
When $k=2$ this distribution is equivalent to the binomial distribution with parameters $m$ and $p={p}_{1}$ (see g05ta).
The variates can be generated with or without using a search table and index. If a search table is used then it is stored with the index in a reference vector and subsequent calls to g05tg with the same parameter values can then use this reference vector to generate further variates. The reference array is generated only for the outcome with greatest probability. The number of successes for the outcome with greatest probability is calculated first as for the binomial distribution (see g05ta); the number of successes for other outcomes are calculated in turn for the remaining reduced multinomial distribution; the number of successes for the final outcome is simply calculated to ensure that the total number of successes is $m$.
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 g05tg.

# References

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:
Some error messages may refer to parameters that are dropped from this interface (LDX) In these cases, an error in another parameter has usually caused an incorrect value to be inferred.
${\mathbf{ifail}}=1$
On entry, ${\mathbf{mode}}\ne 0$, $1$, $2$ or $3$.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{n}}<0$.
${\mathbf{ifail}}=3$
On entry, ${\mathbf{m}}<0$.
${\mathbf{ifail}}=4$
On entry, ${\mathbf{k}}<2$.
${\mathbf{ifail}}=5$
On entry, ${\mathbf{p}}\left[j-1\right]<0.0$ or ${\mathbf{p}}\left[j-1\right]>1.0$ for at least one value of $j$.
The probabilities ${\mathbf{p}}\left[\mathit{j}-1\right]$, for $\mathit{j}=1,2,\dots ,{\mathbf{k}}$, do not add up to $1$.
${\mathbf{ifail}}=6$
The maximum value of ${\mathbf{p}}\left[\mathit{j}-1\right]$, for $\mathit{j}=1,2,\dots ,{\mathbf{k}}$, or m is not the same as when r was set up in a previous call to g05tg with ${\mathbf{mode}}=0$ or $2$.
On entry, the r vector was not initialized correctly, or has been corrupted.
${\mathbf{ifail}}=7$
On entry, lr is too small when ${\mathbf{mode}}=0$ or $2$.
${\mathbf{ifail}}=8$
 On entry, state vector was not initialized or has been corrupted.
${\mathbf{ifail}}=10$
On entry, ${\mathbf{ldx}}<{\mathbf{n}}$.
${\mathbf{ifail}}=-9000$
An error occured, see message report.
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-4000$
Invalid dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-8000$
Negative dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$

Not applicable.

None.