g05pz generates a random two-way table.

# Syntax

C#
```public static void g05pz(
int mode,
int nrow,
int ncol,
int[] totr,
int[] totc,
double[] r,
G05..::..G05State g05state,
int[,] x,
out int ifail
)```
Visual Basic
```Public Shared Sub g05pz ( _
mode As Integer, _
nrow As Integer, _
ncol As Integer, _
totr As Integer(), _
totc As Integer(), _
r As Double(), _
g05state As G05..::..G05State, _
x As Integer(,), _
<OutAttribute> ByRef ifail As Integer _
)```
Visual C++
```public:
static void g05pz(
int mode,
int nrow,
int ncol,
array<int>^ totr,
array<int>^ totc,
array<double>^ r,
G05..::..G05State^ g05state,
array<int,2>^ x,
[OutAttribute] int% ifail
)```
F#
```static member g05pz :
mode : int *
nrow : int *
ncol : int *
totr : int[] *
totc : int[] *
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 two-way table using reference vector set up in a prior call to g05pz.
${\mathbf{mode}}=2$
Set up reference vector and generate two-way table.
Constraint: ${\mathbf{mode}}=0$, $1$ or $2$.
nrow
Type: System..::..Int32
On entry: $m$, the number of rows in the table.
Constraint: ${\mathbf{nrow}}\ge 2$.
ncol
Type: System..::..Int32
On entry: $n$, the number of columns in the table.
Constraint: ${\mathbf{ncol}}\ge 2$.
totr
Type: array<System..::..Int32>[]()[][]
An array of size [nrow]
On entry: the $m$ row totals, ${R}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,m$.
Constraints:
• ${\mathbf{totr}}\left[\mathit{i}-1\right]\ge 0$, for $\mathit{i}=1,2,\dots ,m$;
• $\sum _{i=1}^{m}{\mathbf{totr}}\left[i-1\right]=\sum _{j=1}^{n}{\mathbf{totc}}\left[j-1\right]$;
• ${\sum }_{\mathit{i}}{\mathbf{totr}}\left[\mathit{i}-1\right]>0$, for $\mathit{i}=1,2,\dots ,m$.
totc
Type: array<System..::..Int32>[]()[][]
An array of size [ncol]
On entry: the $n$ column totals, ${C}_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,n$.
Constraints:
• ${\mathbf{totc}}\left[\mathit{j}-1\right]\ge 0$, for $\mathit{j}=1,2,\dots ,n$;
• $\sum _{j=1}^{n}{\mathbf{totc}}\left[j-1\right]=\sum _{i=1}^{m}{\mathbf{totr}}\left[i-1\right]$.
r
Type: array<System..::..Double>[]()[][]
An array of size [lr]
On entry: if ${\mathbf{mode}}=1$, the reference vector from the previous call to g05pz.
On exit: 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, ncol]
Note: dim1 must satisfy the constraint: $\mathrm{dim1}\ge {\mathbf{nrow}}$
On exit: if ${\mathbf{mode}}=1$ or $2$, a pseudorandom two-way $m$ by $n$ table, $X$, with element ${\mathbf{x}}\left[i-1,j-1\right]$ containing the $\left(i,j\right)$th entry in the table such that $\sum _{\mathit{i}=1}^{m}{\mathbf{x}}\left[i-1,j-1\right]={\mathbf{totc}}\left[j-1\right]$ and $\sum _{\mathit{j}=1}^{n}{\mathbf{x}}\left[i-1,j-1\right]={\mathbf{totr}}\left[i-1\right]$
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

Given $m$ row totals ${R}_{i}$ and $n$ column totals ${C}_{j}$ (with $\sum _{i=1}^{m}{R}_{i}=\sum _{j=1}^{n}{C}_{j}=T$, say), g05pz will generate a pseudorandom two-way table of integers such that the row and column totals are satisfied.
The method used is based on that described by Patefield (1981) which is most efficient when $T$ is large relative to the number of table entries $m×n$ (i.e., $T>2mn$). Entries are generated one row at a time and one entry at a time within a row. Each entry is generated using the conditional probability distribution for that entry given the entries in the previous rows and the previous entries in the same row.
A reference vector is used to store computed values that can be reused in the generation of new tables with the same row and column totals. g05pz can be called to simply set up the reference vector, or to generate a two-way table using a reference vector set up in a previous call, or it can combine both functions in a single call.
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 g05pz.

# References

Patefield W M (1981) An efficient method of generating $R×C$ tables with given row and column totals Appl. Stats. 30 91–97

# 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$ or $2$.
${\mathbf{ifail}}=2$
 On entry, ${\mathbf{nrow}}<2$.
${\mathbf{ifail}}=3$
 On entry, ${\mathbf{ncol}}<2$.
${\mathbf{ifail}}=4$
At least one element of totr is negative.
${\mathbf{ifail}}=5$
At least one element of totc is negative.
${\mathbf{ifail}}=6$
The value of nrow or ncol is not the same as when r was set up in a previous call to g05pz with ${\mathbf{mode}}=0$ or $2$.
${\mathbf{ifail}}=7$
 On entry, lr is too small when ${\mathbf{mode}}=0$ or $2$ (see [Parameters]).
${\mathbf{ifail}}=8$
 On entry, state vector was not initialized or has been corrupted.
${\mathbf{ifail}}=15$
The arrays totr and totc do not sum to the same total.
${\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}}〉$

None.

None.