﻿ g05td Method
g05td generates a vector of pseudorandom integers from a discrete distribution with a given PDF (probability density function) or CDF (cumulative distribution function) $p$.

# Syntax

C#
```public static void g05td(
int mode,
int n,
double[] p,
int np,
int ip1,
int itype,
double[] r,
G05..::..G05State g05state,
int[] x,
out int ifail
)```
Visual Basic
```Public Shared Sub g05td ( _
mode As Integer, _
n As Integer, _
p As Double(), _
np As Integer, _
ip1 As Integer, _
itype As Integer, _
r As Double(), _
g05state As G05..::..G05State, _
x As Integer(), _
<OutAttribute> ByRef ifail As Integer _
)```
Visual C++
```public:
static void g05td(
int mode,
int n,
array<double>^ p,
int np,
int ip1,
int itype,
array<double>^ r,
G05..::..G05State^ g05state,
array<int>^ x,
[OutAttribute] int% ifail
)```
F#
```static member g05td :
mode : int *
n : int *
p : float[] *
np : int *
ip1 : int *
itype : 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 variates using reference vector set up in a prior call to g05td.
${\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$.
p
Type: array<System..::..Double>[]()[][]
An array of size [np]
On entry: the PDF or CDF of the distribution.
Constraints:
• $0.0\le {\mathbf{p}}\left[\mathit{i}-1\right]\le 1.0$, for $\mathit{i}=1,2,\dots ,{\mathbf{np}}$;
• if ${\mathbf{itype}}=1$, $\sum _{\mathit{i}=1}^{{\mathbf{np}}}{\mathbf{p}}\left[\mathit{i}-1\right]=1.0$;
• if ${\mathbf{itype}}=2$, ${\mathbf{p}}\left[\mathit{i}-1\right]<{\mathbf{p}}\left[j-1\right]\text{, ​}\mathit{i}.
np
Type: System..::..Int32
On entry: the number of values supplied in p defining the PDF or CDF of the discrete distribution.
Constraint: ${\mathbf{np}}>0$.
ip1
Type: System..::..Int32
On entry: the value of the variate, a whole number, to which the probability in ${\mathbf{p}}\left[0\right]$ corresponds.
itype
Type: System..::..Int32
On entry: indicates the type of information contained in p.
${\mathbf{itype}}=1$
p contains a probability distribution function (PDF).
${\mathbf{itype}}=2$
p contains a cumulative distribution function (CDF).
Constraint: ${\mathbf{itype}}=1$ or $2$.
r
Type: array<System..::..Double>[]()[][]
An array of size [lr]
On entry: if ${\mathbf{mode}}=1$, the reference vector from the previous call to g05td.
On exit: the reference vector.
g05state
Type: NagLibrary..::..G05..::..G05State
An Object of type G05.G05State.
x
Type: array<System..::..Int32>[]()[][]
An array of size [n]
On exit: contains $n$ pseudorandom numbers from the specified discrete 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

g05td generates a sequence of $n$ integers ${x}_{i}$, from a discrete distribution defined by information supplied in p. This may either be the PDF or CDF of the distribution. A reference vector is first set up to contain the CDF of the distribution in its higher elements, followed by an index.
Setting up the reference vector and subsequent generation of variates can each be performed by separate calls to g05td or may be combined 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 g05td.

# References

Kendall M G and Stuart A (1969) The Advanced Theory of Statistics (Volume 1) (3rd Edition) Griffin
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{mode}}\ne 0$, $1$ or $2$.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{n}}<0$.
${\mathbf{ifail}}=3$
With ${\mathbf{itype}}=1$, ${\mathbf{p}}\left[j-1\right]<0$ for at least one value of $j$.
With ${\mathbf{itype}}=1$, the sum of ${\mathbf{p}}\left[\mathit{j}-1\right]$, for $\mathit{j}=1,2,\dots ,{\mathbf{np}}$, does not equal $1$.
With ${\mathbf{itype}}=2$, the values of ${\mathbf{p}}\left[j-1\right]$ are not all in non-descending order.
${\mathbf{ifail}}=4$
On entry, ${\mathbf{np}}<1$.
${\mathbf{ifail}}=6$
On entry, ${\mathbf{itype}}\ne 1$ or $2$.
${\mathbf{ifail}}=7$
The value of np, itype or ip1 is not the same as when r was set up in a previous call to g05td with ${\mathbf{mode}}=0$ or $2$.
On entry, the r vector was not initialized correctly, or has been corrupted.
${\mathbf{ifail}}=8$
On entry, lr is too small when ${\mathbf{mode}}=0$ or $2$.
${\mathbf{ifail}}=9$
 On entry, state vector was not initialized or has been corrupted.
${\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.

None.