g05sp generates a vector of pseudorandom numbers from a triangular distribution with parameters ${x}_{\mathrm{min}}$, ${x}_{\mathrm{med}}$ and ${x}_{\mathrm{max}}$.

# Syntax

C#
```public static void g05sp(
int n,
double xmin,
double xmed,
double xmax,
G05..::..G05State g05state,
double[] x,
out int ifail
)```
Visual Basic
```Public Shared Sub g05sp ( _
n As Integer, _
xmin As Double, _
xmed As Double, _
xmax As Double, _
g05state As G05..::..G05State, _
x As Double(), _
<OutAttribute> ByRef ifail As Integer _
)```
Visual C++
```public:
static void g05sp(
int n,
double xmin,
double xmed,
double xmax,
G05..::..G05State^ g05state,
array<double>^ x,
[OutAttribute] int% ifail
)```
F#
```static member g05sp :
n : int *
xmin : float *
xmed : float *
xmax : 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$.
xmin
Type: System..::..Double
On entry: the end point ${x}_{\mathrm{min}}$ of the triangular distribution.
xmed
Type: System..::..Double
On entry: the median of the distribution ${x}_{\mathrm{med}}$ (also the location of the vertex of the triangular distribution at which the PDF reaches a maximum).
Constraint: ${\mathbf{xmed}}\ge {\mathbf{xmin}}$.
xmax
Type: System..::..Double
On entry: the end point ${x}_{\mathrm{max}}$ of the triangular distribution.
Constraint: ${\mathbf{xmax}}\ge {\mathbf{xmed}}$.
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 triangular 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 triangular distribution has a PDF (probability density function) that is triangular in profile. The base of the triangle ranges from $x={x}_{\mathrm{min}}$ to $x={x}_{\mathrm{max}}$ and the PDF has a maximum value of $\frac{2}{{x}_{\mathrm{max}}-{x}_{\mathrm{min}}}$ at $x={x}_{\mathrm{med}}$. If ${x}_{\mathrm{min}}={x}_{\mathrm{med}}={x}_{\mathrm{max}}$ then $x={x}_{\mathrm{med}}$ with probability 1; otherwise the triangular distribution has PDF:
 $fx=x-xminxmed-xmin×2xmax-xmin​ if ​xmin≤x≤xmed,fx=xmax-xxmax-xmed×2xmax-xmin​ if ​xmed
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 g05sp.

# 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:
${\mathbf{ifail}}=1$
On entry, ${\mathbf{n}}<0$.
${\mathbf{ifail}}=3$
On entry, ${\mathbf{xmed}}<{\mathbf{xmin}}$.
${\mathbf{ifail}}=4$
On entry, ${\mathbf{xmax}}<{\mathbf{xmed}}$.
${\mathbf{ifail}}=5$
 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.