nag_rngs_gen_discrete (g05mzc) (PDF version)
g05 Chapter Contents
g05 Chapter Introduction
NAG C Library Manual

NAG Library Function Document

nag_rngs_gen_discrete (g05mzc)

+ Contents

    1  Purpose
    7  Accuracy

1  Purpose

nag_rngs_gen_discrete (g05mzc) generates a vector of pseudorandom integers from a discrete distribution with a given PDF (probability density function) or CDF (cumulative distribution function) p.

2  Specification

#include <nag.h>
#include <nagg05.h>
void  nag_rngs_gen_discrete (Integer mode, const double p[], Integer np, Integer ip1, Nag_ComputeType comp_type, Integer n, Integer x[], Integer igen, Integer iseed[], double r[], NagError *fail)

3  Description

nag_rngs_gen_discrete (g05mzc) generates a sequence of n integers xi, 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. The full specifications of the reference vector are as follows.
Setting up the reference vector and subsequent generation of variates can each be performed by separate calls to nag_rngs_gen_discrete (g05mzc) or may be combined in a single call.
One of the initialization functions nag_rngs_init_repeatable (g05kbc) (for a repeatable sequence if computed sequentially) or nag_rngs_init_nonrepeatable (g05kcc) (for a non-repeatable sequence) must be called prior to the first call to nag_rngs_gen_discrete (g05mzc).

4  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

5  Arguments

1:     modeIntegerInput
On entry: a code for selecting the operation to be performed by the function.
Set up reference vector only.
Generate variates using reference vector set up in a prior call to nag_rngs_gen_discrete (g05mzc).
Set up reference vector and generate variates.
Constraint: mode=0, 1 or 2.
2:     p[np]const doubleInput
On entry: the PDF or CDF of the distribution.
3:     npIntegerInput
On entry: the number of values supplied in p defining the PDF or CDF of the discrete distribution.
Constraint: np>0.
4:     ip1IntegerInput
On entry: the value of the variate, a whole number, to which the probability in p[0] corresponds.
5:     comp_typeNag_ComputeTypeInput
On entry: indicates the type of information contained in p.
p contains a probability distribution function (PDF).
p contains a cumulative distribution function (CDF).
Constraint: comp_type=Nag_Compute_1 or Nag_Compute_2.
6:     nIntegerInput
On entry: n, the number of pseudorandom numbers to be generated.
Constraint: n1.
7:     x[n]IntegerOutput
On exit: contains n pseudorandom numbers from the specified discrete distribution.
8:     igenIntegerInput
On entry: must contain the identification number for the generator to be used to return a pseudorandom number and should remain unchanged following initialization by a prior call to nag_rngs_init_repeatable (g05kbc) or nag_rngs_init_nonrepeatable (g05kcc).
9:     iseed[4]IntegerCommunication Array
On entry: contains values which define the current state of the selected generator.
On exit: contains updated values defining the new state of the selected generator.
10:   r[8+1.4×np]doubleCommunication Array
On entry: if mode=1, the reference vector from the previous call to nag_rngs_gen_discrete (g05mzc).
On exit: the reference vector.
11:   failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

6  Error Indicators and Warnings

On entry, argument value had an illegal value.
On entry, mode=value.
Constraint: mode=0, 1 or 2.
On entry, n=value.
Constraint: n1.
On entry, np=value.
Constraint: np>0.
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
On entry, comp_type=Nag_Compute_2 and the values in p are not all in non-descending order.
comp_type is not the same as when r was set up in a previous call. Previous value of comp_type=value and comp_type=value.
np or ip1 is not the same as when r was set up in a previous call. Previous value of np=value and np=value. Previous value of ip1=value and ip1=value.
On entry, at least one element of the vector p is negative. The first found is at position i=value, p[i-1]=value.
On entry, the sum of the elements of p is not one. The difference from unity in the summation is: value.

7  Accuracy

Not applicable.

8  Further Comments


9  Example

This example prints 20 pseudorandom variates from a discrete distribution whose PDF, fn, is defined as follows:
n fn -5 0.01 -4 0.02 -3 0.04 -2 0.08 -1 0.20 -0 0.30 -1 0.20 -2 0.08 -3 0.04 -4 0.02 -5 0.01
The reference vector is set up and and the variates are generated by a single call to nag_rngs_gen_discrete (g05mzc), after initialization by nag_rngs_init_repeatable (g05kbc).

9.1  Program Text

Program Text (g05mzce.c)

9.2  Program Data


9.3  Program Results

Program Results (g05mzce.r)

nag_rngs_gen_discrete (g05mzc) (PDF version)
g05 Chapter Contents
g05 Chapter Introduction
NAG C Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2012