# ASA159 Random generation of a table.

ASA159 is a C++ library which constructs a table having given row and column sums, by Michael Patefield;

ASA159 is Applied Statistics Algorithm 159. Source code for many Applied Statistics Algorithms is available through STATLIB.

ASA159 accepts a table shape (the number of rows and columns), and two vectors, the lists of row and column sums. There may be 0, 1, or many tables with nonnegative, integral entries that have the given shape and sums. The routine will report the case if there are no candidates. If there is at least one candidate, then the routine will choose one, uniformly over the number of distinct candidates.

For example, suppose M = 2, N = 3, row sum = (/ 6, 5 /) and column sum = (/ 3, 4, 4 /), then here are two tables that satisfy the constraints:

```        1  2  3
2  2  1
```
and
```        3  0  3
0  4  1
```

### Languages:

ASA159 is available in a C version and a C++ version and a FORTRAN90 version and a MATLAB version.

### Related Data and Programs:

ASA144, a C++ library which randomly generates an RxC contingency table.

SUBSET, a C++ library which includes a routine called I4MAT_01_ROWCOLSUM which generates a (0,1) matrix with given row and column sums.

### Author:

Original FORTRAN77 version by Michael Patefield; Matlab version by John Burkardt.

### Reference:

1. Michael Patefield,
Algorithm AS 159: An Efficient Method of Generating RXC Tables with Given Row and Column Totals,
Applied Statistics,
Volume 30, Number 1, 1981, pages 91-97.

### List of Routines:

• I4_MAX returns the maximum of two I4's.
• I4_MIN returns the minimum of two I4's.
• I4MAT_PRINT prints an I4MAT, with an optional title.
• I4MAT_PRINT_SOME prints some of an I4MAT.
• I4VEC_PRINT prints an I4VEC.
• I4VEC_SUM sums the entries of an I4VEC.
• R8_UNIFORM_01 is a unit pseudorandom R8.
• RCONT2 constructs a random two-way contingency table with given sums.
• S_LEN_TRIM returns the length of a string to the last nonblank.
• TIMESTAMP prints the current YMDHMS date as a time stamp.

You can go up one level to the C++ source codes.

Last revised on 10 March 2009.