nag_rand_2_way_table (g05pzc) (PDF version)
g05 Chapter Contents
g05 Chapter Introduction
NAG Library Manual

NAG Library Function Document

nag_rand_2_way_table (g05pzc)


    1  Purpose
    7  Accuracy

1  Purpose

nag_rand_2_way_table (g05pzc) generates a random two-way table.

2  Specification

#include <nag.h>
#include <nagg05.h>
void  nag_rand_2_way_table (Nag_ModeRNG mode, Integer nrow, Integer ncol, const Integer totr[], const Integer totc[], double r[], Integer lr, Integer state[], Integer x[], Integer pdx, NagError *fail)

3  Description

Given m row totals Ri and n column totals Cj (with i=1m Ri=j=1n Cj=T, say), nag_rand_2_way_table (g05pzc) will generate a pseudorandom two-way table of integers such that the row and column totals are satisfied.
The method used is based on that described by Patefield (1981) which is most efficient when T is large relative to the number of table entries m×n (i.e., T>2mn). Entries are generated one row at a time and one entry at a time within a row. Each entry is generated using the conditional probability distribution for that entry given the entries in the previous rows and the previous entries in the same row.
A reference vector is used to store computed values that can be reused in the generation of new tables with the same row and column totals. nag_rand_2_way_table (g05pzc) can be called to simply set up the reference vector, or to generate a two-way table using a reference vector set up in a previous call, or it can combine both functions in a single call.
One of the initialization functions nag_rand_init_repeatable (g05kfc) (for a repeatable sequence if computed sequentially) or nag_rand_init_nonrepeatable (g05kgc) (for a non-repeatable sequence) must be called prior to the first call to nag_rand_2_way_table (g05pzc).

4  References

Patefield W M (1981) An efficient method of generating R×C tables with given row and column totals Appl. Stats. 30 91–97

5  Arguments

1:     mode Nag_ModeRNGInput
On entry: a code for selecting the operation to be performed by the function.
Set up reference vector only.
Generate two-way table using reference vector set up in a prior call to nag_rand_2_way_table (g05pzc).
Set up reference vector and generate two-way table.
Constraint: mode=Nag_InitializeReference, Nag_GenerateFromReference or Nag_InitializeAndGenerate.
2:     nrow IntegerInput
On entry: m, the number of rows in the table.
Constraint: nrow2.
3:     ncol IntegerInput
On entry: n, the number of columns in the table.
Constraint: ncol2.
4:     totr[nrow] const IntegerInput
On entry: the m row totals, Ri, for i=1,2,,m.
  • totr[i-1]0, for i=1,2,,m;
  • i=1mtotr[i-1]=j=1ntotc[j-1];
  • i totr[i-1]>0, for i=1,2,,m.
5:     totc[ncol] const IntegerInput
On entry: the n column totals, Cj, for j=1,2,,n.
  • totc[j-1]0, for j=1,2,,n;
  • j=1ntotc[j-1]=i=1mtotr[i-1].
6:     r[lr] doubleCommunication Array
On entry: if mode=Nag_GenerateFromReference, the reference vector from the previous call to nag_rand_2_way_table (g05pzc).
On exit: the reference vector.
7:     lr IntegerInput
On entry: the dimension of the array r.
Constraint: lri=1mtotr[i-1]+5.
8:     state[dim] IntegerCommunication Array
Note: the dimension, dim, of this array is dictated by the requirements of associated functions that must have been previously called. This array MUST be the same array passed as argument state in the previous call to nag_rand_init_repeatable (g05kfc) or nag_rand_init_nonrepeatable (g05kgc).
On entry: contains information on the selected base generator and its current state.
On exit: contains updated information on the state of the generator.
9:     x[nrow×pdx] IntegerOutput
On exit: if mode=Nag_GenerateFromReference or Nag_InitializeAndGenerate, a pseudorandom two-way m by n table, X, with element x[i-1×pdx+j-1] containing the i,jth entry in the table such that i=1 m x[i-1×pdx+j-1]=totc[j-1] and j=1 n x[i-1×pdx+j-1]=totr[i-1]
10:   pdx IntegerInput
On entry: the stride separating matrix column elements in the array x.
Constraint: pdxncol.
11:   fail NagError *Input/Output
The NAG error argument (see Section 2.7 in How to Use the NAG Library and its Documentation).

6  Error Indicators and Warnings

Dynamic memory allocation failed.
See Section in How to Use the NAG Library and its Documentation for further information.
On entry, argument value had an illegal value.
On entry, lr is not large enough, lr=value: minimum length required =value.
On entry, ncol=value.
Constraint: ncol2.
On entry, nrow=value.
Constraint: nrow2.
On entry, pdx=value and ncol=value.
Constraint: pdxncol.
On entry, at least one element of totr is negative or totr sums to zero.
On entry, totc has at least one negative element.
On entry, the arrays totr and totc do not sum to the same total: totr array total is value, totc array total is value.
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.
An unexpected error has been triggered by this function. Please contact NAG.
See Section 2.7.6 in How to Use the NAG Library and its Documentation for further information.
On entry, state vector has been corrupted or not initialized.
Your licence key may have expired or may not have been installed correctly.
See Section 2.7.5 in How to Use the NAG Library and its Documentation for further information.
nrow or ncol is not the same as when r was set up in a previous call.
Previous value of nrow=value and nrow=value.
Previous value of ncol=value and ncol=value.

7  Accuracy


8  Parallelism and Performance

nag_rand_2_way_table (g05pzc) is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
Please consult the x06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this function. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

9  Further Comments


10  Example

Following initialization of the pseudorandom number generator by a call to nag_rand_init_repeatable (g05kfc), this example generates and prints a 4 by 3 two-way table, with row totals of 9, 11, 7 and 23 respectively, and column totals of 16, 17 and 17 respectively.

10.1  Program Text

Program Text (g05pzce.c)

10.2  Program Data


10.3  Program Results

Program Results (g05pzce.r)

nag_rand_2_way_table (g05pzc) (PDF version)
g05 Chapter Contents
g05 Chapter Introduction
NAG Library Manual

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