g05sj generates a vector of pseudorandom numbers taken from a gamma distribution with parameters $a$ and $b$.

# Syntax

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

#### Parameters

n
Type: System..::..Int32
On entry: $n$, the number of pseudorandom numbers to be generated.
Constraint: ${\mathbf{n}}\ge 0$.
a
Type: System..::..Double
On entry: $a$, the parameter of the gamma distribution.
Constraint: ${\mathbf{a}}>0.0$.
b
Type: System..::..Double
On entry: $b$, the parameter of the gamma distribution.
Constraint: ${\mathbf{b}}>0.0$.
g05state
Type: NagLibrary..::..G05..::..G05State
An Object of type G05.G05State.
x
Type: array<System..::..Double>[]()[][]
An array of size [n]
On exit: the $n$ pseudorandom numbers from the specified gamma 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

The gamma distribution has PDF (probability density function)
 $fx=1baΓaxa-1e-x/bif ​x≤0; a,b>0fx=0otherwise.$
One of three algorithms is used to generate the variates depending upon the value of $a$:
 (i) if $a<1$, a switching algorithm described by Dagpunar (1988) (called G6) is used. The target distributions are ${f}_{1}\left(x\right)=ca{x}^{a-1}/{t}^{a}$ and ${f}_{2}\left(x\right)=\left(1-c\right){e}^{-\left(x-t\right)}$, where $c=t/\left(t+a{e}^{-t}\right)$, and the switching parameter, $t$, is taken as $1-a$. This is similar to Ahrens and Dieter's GS algorithm (see Ahrens and Dieter (1974)) in which $t=1$; (ii) if $a=1$, the gamma distribution reduces to the exponential distribution and the method based on the logarithmic transformation of a uniform random variate is used; (iii) if $a>1$, the algorithm given by Best (1978) is used. This is based on using a Student's $t$-distribution with two degrees of freedom as the target distribution in an envelope rejection method.
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 g05sj.

# References

Ahrens J H and Dieter U (1974) Computer methods for sampling from gamma, beta, Poisson and binomial distributions Computing 12 223–46
Best D J (1978) Letter to the Editor Appl. Statist. 27 181
Dagpunar J (1988) Principles of Random Variate Generation Oxford University Press
Hastings N A J and Peacock J B (1975) Statistical Distributions Butterworth

# Error Indicators and Warnings

Errors or warnings detected by the method:
${\mathbf{ifail}}=1$
On entry, ${\mathbf{n}}<0$.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{a}}\le 0.0$.
${\mathbf{ifail}}=3$
On entry, ${\mathbf{b}}\le 0.0$.
${\mathbf{ifail}}=4$
 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.