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_int_general (g05td)

Purpose

nag_rand_int_general (g05td) generates a vector of pseudorandom integers from a discrete distribution with a given PDF (probability density function) or CDF (cumulative distribution function) pp.

Syntax

[r, state, x, ifail] = g05td(mode, n, p, ip1, itype, r, state, 'np', np)
[r, state, x, ifail] = nag_rand_int_general(mode, n, p, ip1, itype, r, state, 'np', np)

Description

nag_rand_int_general (g05td) generates a sequence of nn integers xixi, 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.
Setting up the reference vector and subsequent generation of variates can each be performed by separate calls to nag_rand_int_general (g05td) or may be combined in a single call.
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_int_general (g05td).

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

Parameters

Compulsory Input Parameters

1:     mode – int64int32nag_int scalar
A code for selecting the operation to be performed by the function.
mode = 0mode=0
Set up reference vector only.
mode = 1mode=1
Generate variates using reference vector set up in a prior call to nag_rand_int_general (g05td).
mode = 2mode=2
Set up reference vector and generate variates.
mode = 3mode=3
Generate variates without using the reference vector.
Constraint: mode = 0mode=0, 11, 22 or 33.
2:     n – int64int32nag_int scalar
nn, the number of pseudorandom numbers to be generated.
Constraint: n0n0.
3:     p(np) – double array
np, the dimension of the array, must satisfy the constraint np > 0np>0.
The PDF or CDF of the distribution.
Constraints:
  • 0.0p(i)1.00.0pi1.0, for i = 1,2,,npi=1,2,,np;
  • if itype = 1itype=1, i = 1np p(i) = 1.0 i=1 np pi=1.0;
  • if itype = 2itype=2, p(i) < p(j), ​i < j​ and ​p(np) = 1.0pi<pj, ​i<j​ and ​pnp=1.0.
4:     ip1 – int64int32nag_int scalar
The value of the variate, a whole number, to which the probability in p(1)p1 corresponds.
5:     itype – int64int32nag_int scalar
Indicates the type of information contained in p.
itype = 1itype=1
p contains a probability distribution function (PDF).
itype = 2itype=2
p contains a cumulative distribution function (CDF).
Constraint: itype = 1itype=1 or 22.
6:     r(lr) – double array
lr, the dimension of the array, must satisfy the constraint
  • if mode = 0mode=0 or 22, lrnp + 8lrnp+8;
  • if mode = 1mode=1, lr should remain unchanged from the previous call to nag_rand_int_general (g05td).
If mode = 1mode=1, the reference vector from the previous call to nag_rand_int_general (g05td).
7:     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

1:     np – int64int32nag_int scalar
Default: The dimension of the array p.
The number of values supplied in p defining the PDF or CDF of the discrete distribution.
Constraint: np > 0np>0.

Input Parameters Omitted from the MATLAB Interface

lr

Output Parameters

1:     r(lr) – double array
The reference vector.
2:     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 updated information on the state of the generator.
3:     x(n) – int64int32nag_int array
Contains nn pseudorandom numbers from the specified discrete distribution.
4:     ifail – int64int32nag_int scalar
ifail = 0ifail=0 unless the function detects an error (see [Error Indicators and Warnings]).

Error Indicators and Warnings

Errors or warnings detected by the function:
  ifail = 1ifail=1
On entry, mode0mode0, 11 or 22.
  ifail = 2ifail=2
On entry, n < 0n<0.
  ifail = 3ifail=3
With itype = 1itype=1, p(j) < 0pj<0 for at least one value of jj.
With itype = 1itype=1, the sum of p(j)pj, for j = 1,2,,npj=1,2,,np, does not equal 11.
With itype = 2itype=2, the values of p(j)pj are not all in non-descending order.
  ifail = 4ifail=4
On entry, np < 1np<1.
  ifail = 6ifail=6
On entry, itype1itype1 or 22.
  ifail = 7ifail=7
The value of np, itype or ip1 is not the same as when r was set up in a previous call to nag_rand_int_general (g05td) with mode = 0mode=0 or 22.
On entry, the r vector was not initialized correctly, or has been corrupted.
  ifail = 8ifail=8
On entry, lr is too small when mode = 0mode=0 or 22.
  ifail = 9ifail=9
On entry,state vector was not initialized or has been corrupted.

Accuracy

Not applicable.

Further Comments

None.

Example

function nag_rand_int_general_example
% Initialize the seed
seed = [int64(1762543)];
% genid and subid identify the base generator
genid = int64(1);
subid =  int64(1);
mode = int64(2);
n = int64(20);
p = [0.01;
     0.02;
     0.04;
     0.08;
     0.2;
     0.3;
     0.2;
     0.08;
     0.04;
     0.02;
     0.01];
ip1 = int64(-5);
itype = int64(1);
r = zeros(60, 1);
% Initialize the generator to a repeatable sequence
[state, ifail] = nag_rand_init_repeat(genid, subid, seed);
[r, state, x, ifail] = nag_rand_int_general(mode, n, p, ip1, itype, r, state)
 

r =

   1.0e+03 *

    9.9995
    0.0605
    0.0115
   -0.0055
    0.0015
    0.0115
   -0.0065
    0.0410
    0.0000
    0.0000
    0.0001
    0.0002
    0.0004
    0.0006
    0.0009
    0.0009
    0.0010
    0.0010
    0.0010
    0.0015
    0.0025
    0.0035
    0.0045
    0.0045
    0.0045
    0.0045
    0.0055
    0.0055
    0.0055
    0.0055
    0.0055
    0.0055
    0.0055
    0.0055
    0.0065
    0.0065
    0.0065
    0.0065
    0.0065
    0.0065
    0.0065
    0.0065
    0.0065
    0.0065
    0.0065
    0.0065
    0.0075
    0.0075
    0.0075
    0.0075
    0.0075
    0.0075
    0.0075
    0.0075
    0.0085
    0.0085
    0.0085
    0.0085
    0.0095
    0.0105


state =

                   17
                 1234
                    1
                    0
                 6694
                27818
                10435
                15383
                17917
                13895
                19930
                    8
                    0
                 1234
                    1
                    1
                 1234


x =

                    0
                   -2
                    1
                    1
                   -2
                    0
                    0
                    1
                    0
                    1
                   -3
                   -1
                    0
                   -3
                    0
                   -1
                   -1
                    5
                    2
                    0


ifail =

                    0


function g05td_example
% Initialize the seed
seed = [int64(1762543)];
% genid and subid identify the base generator
genid = int64(1);
subid =  int64(1);
mode = int64(2);
n = int64(20);
p = [0.01;
     0.02;
     0.04;
     0.08;
     0.2;
     0.3;
     0.2;
     0.08;
     0.04;
     0.02;
     0.01];
ip1 = int64(-5);
itype = int64(1);
r = zeros(60, 1);
% Initialize the generator to a repeatable sequence
[state, ifail] = g05kf(genid, subid, seed);
[r, state, x, ifail] = g05td(mode, n, p, ip1, itype, r, state)
 

r =

   1.0e+03 *

    9.9995
    0.0605
    0.0115
   -0.0055
    0.0015
    0.0115
   -0.0065
    0.0410
    0.0000
    0.0000
    0.0001
    0.0002
    0.0004
    0.0006
    0.0009
    0.0009
    0.0010
    0.0010
    0.0010
    0.0015
    0.0025
    0.0035
    0.0045
    0.0045
    0.0045
    0.0045
    0.0055
    0.0055
    0.0055
    0.0055
    0.0055
    0.0055
    0.0055
    0.0055
    0.0065
    0.0065
    0.0065
    0.0065
    0.0065
    0.0065
    0.0065
    0.0065
    0.0065
    0.0065
    0.0065
    0.0065
    0.0075
    0.0075
    0.0075
    0.0075
    0.0075
    0.0075
    0.0075
    0.0075
    0.0085
    0.0085
    0.0085
    0.0085
    0.0095
    0.0105


state =

                   17
                 1234
                    1
                    0
                 6694
                27818
                10435
                15383
                17917
                13895
                19930
                    8
                    0
                 1234
                    1
                    1
                 1234


x =

                    0
                   -2
                    1
                    1
                   -2
                    0
                    0
                    1
                    0
                    1
                   -3
                   -1
                    0
                   -3
                    0
                   -1
                   -1
                    5
                    2
                    0


ifail =

                    0



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–2013