hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_rand_dist_triangular (g05sp)

 Contents

    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example

Purpose

nag_rand_dist_triangular (g05sp) generates a vector of pseudorandom numbers from a triangular distribution with parameters xmin, xmed and xmax.

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=xmin to x=xmax and the PDF has a maximum value of 2xmax-xmin  at x=xmed. If xmin=xmed=xmax then x=xmed with probability 1; otherwise the triangular distribution has PDF:
fx = x-xmin xmed-xmin × 2 xmax-xmin ​ if ​xminxxmed, fx= xmax-x xmax-xmed ×2xmax-xmin ​ if ​xmed<xxmax, fx=0 ​ otherwise.  
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:     n int64int32nag_int scalar
n, the number of pseudorandom numbers to be generated.
Constraint: n0.
2:     xmin – double scalar
The end point xmin of the triangular distribution.
3:     xmed – double scalar
The median of the distribution xmed (also the location of the vertex of the triangular distribution at which the PDF reaches a maximum).
Constraint: xmedxmin.
4:     xmax – double scalar
The end point xmax of the triangular distribution.
Constraint: xmaxxmed.
5:     state: 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.

Optional Input Parameters

None.

Output Parameters

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

Error Indicators and Warnings

Errors or warnings detected by the function:
   ifail=1
Constraint: n0.
   ifail=3
Constraint: xmedxmin.
   ifail=4
Constraint: xmaxxmed.
   ifail=5
On entry, state vector has been corrupted or not initialized.
   ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
   ifail=-399
Your licence key may have expired or may not have been installed correctly.
   ifail=-999
Dynamic memory allocation failed.

Accuracy

Not applicable.

Further Comments

None.

Example

This example prints five pseudorandom numbers from a triangular distribution with parameters xmin=-1.0, xmed=0.5 and xmax=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


PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015