Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_rand_dist_triangular (g05sp)

## Purpose

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

## Syntax

[state, x, ifail] = g05sp(n, xmin, xmed, xmax, state)
[state, x, ifail] = nag_rand_dist_triangular(n, xmin, xmed, xmax, state)

## 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-xmin xmed-xmin × 2 xmax-xmin ​ if ​xmin≤x≤xmed, fx= xmax-x xmax-xmed ×2xmax-xmin ​ if ​xmed
One of the initialization functions nag_rand_init_repeat (g05kf) (for a repeatable sequence if computed sequentially) or nag_rand_init_nonrepeat (g05kg) (for a non-repeatable sequence) must be called prior to the first call to nag_rand_dist_triangular (g05sp).

## References

Knuth D E (1981) The Art of Computer Programming (Volume 2) (2nd Edition) Addison–Wesley

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{n}$int64int32nag_int scalar
$n$, the number of pseudorandom numbers to be generated.
Constraint: ${\mathbf{n}}\ge 0$.
2:     $\mathrm{xmin}$ – double scalar
The end point ${x}_{\mathrm{min}}$ of the triangular distribution.
3:     $\mathrm{xmed}$ – double scalar
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}}$.
4:     $\mathrm{xmax}$ – double scalar
The end point ${x}_{\mathrm{max}}$ of the triangular distribution.
Constraint: ${\mathbf{xmax}}\ge {\mathbf{xmed}}$.
5:     $\mathrm{state}\left(:\right)$int64int32nag_int array
Note: the actual argument supplied must be the array state supplied to the initialization routines nag_rand_init_repeat (g05kf) or nag_rand_init_nonrepeat (g05kg).
Contains information on the selected base generator and its current state.

None.

### Output Parameters

1:     $\mathrm{state}\left(:\right)$int64int32nag_int array
Contains updated information on the state of the generator.
2:     $\mathrm{x}\left({\mathbf{n}}\right)$ – double array
The $n$ pseudorandom numbers from the specified triangular distribution.
3:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{0}}$ unless the function detects an error (see Error Indicators and Warnings).

## Error Indicators and Warnings

Errors or warnings detected by the function:
${\mathbf{ifail}}=1$
Constraint: ${\mathbf{n}}\ge 0$.
${\mathbf{ifail}}=3$
Constraint: ${\mathbf{xmed}}\ge {\mathbf{xmin}}$.
${\mathbf{ifail}}=4$
Constraint: ${\mathbf{xmax}}\ge {\mathbf{xmed}}$.
${\mathbf{ifail}}=5$
On entry, state vector has been corrupted or not initialized.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

Not applicable.

None.

## Example

This example prints five pseudorandom numbers from a triangular distribution with parameters ${x}_{\mathrm{min}}=-1.0$, ${x}_{\mathrm{med}}=0.5$ and ${x}_{\mathrm{max}}=1.0$, generated by a single call to nag_rand_dist_triangular (g05sp), after initialization by nag_rand_init_repeat (g05kf).
```function g05sp_example

fprintf('g05sp example results\n\n');

% Initialize the base generator to a repeatable sequence
seed  = [int64(1762543)];
genid = int64(1);
subid = int64(1);
[state, ifail] = g05kf( ...
genid, subid, seed);

% Number of variates
n = int64(5);

% Parameters
xmin = -1;
xmed = 0.5;
xmax = 1;

% Generate variates from a triangular distribution
[state, x, ifail] = g05sp( ...
n, xmin, xmed, xmax, state);

disp('Variates');
disp(x);

```
```g05sp example results

Variates
0.3817
-0.4348
0.4960
0.5509
-0.4398

```