src/rcont.c
int rcont1(double* matrix, int nr, const double* r, int nc, const double* c,
           int** work, bitgen_t* rstate) {
  int status = 0;
  if (*work == 0) {
    double nd = 0;
    status = rcont_check(&nd, matrix, nr, r, nc, c);
    if (status != 0)
      return status;

    int n = (int)nd;
    *work = (int*)malloc(sizeof(int) * (n + 1));
    if (!*work) {
      status = 1;
      return status;
    }
    *work[0] = n;
    int* ymap = *work + 1;
    for (int i = 0; i < nc; ++i) {
      int ci = (int)c[i];
      while(ci--)
        *ymap++ = i;
    }
  }

  int n = *work[0];
  int* ymap = *work + 1;

  // shuffle ymap
  for (int i=n-1; i>0; --i) {
    int j = random_interval(rstate, i);
    int tmp = ymap[j];
    ymap[j] = ymap[i];
    ymap[i] = tmp;
  }

  // clear table
  for (int i = 0, nrc = (nr * nc); i < nrc; ++i)
    matrix[i] = 0;

  // fill table
  for (int ir = 0; ir < nr; ++ir) {
    int ri = (int)r[ir];
    while(ri--)
      *ptr(matrix, nr, nc, ir, *ymap++) += 1;
  }

  return 0;
}
/*
  Generate random two-way table with given marginal totals.

  Shuffling algorithm with O(N) complexity in space and time for a table with
  N entries in total. The algorithm performs poorly for large N, but is insensitive
  to the number K of table cells.

  This function uses a work space that is allocated into the argument work
  (which must be zero initialised) and has to freed by the user.
*/
Generate random two-way table with given marginal totals.
Shuffling algorithm with O(N) complexity in space and time for a table with
N entries in total. The algorithm performs poorly for large N, but is insensitive
to the number K of table cells.

This function uses a work space that is allocated into the argument work
(which must be zero initialised) and has to freed by the user.
Generate random two - way table with given marginal totals . Shuffling algorithm with O ( N ) complexity in space and time for a table with N entries in total . The algorithm performs poorly for large N but is insensitive to the number K of table cells . This function uses a work space that is allocated into the argument work ( which must be zero initialised ) and has to freed by the user .
int rcont1(double* matrix, int nr, const double* r, int nc, const double* c,
           int** work, bitgen_t* rstate) {
  int status = 0;
  if (*work == 0) {
    double nd = 0;
    status = rcont_check(&nd, matrix, nr, r, nc, c);
    if (status != 0)
      return status;
    int n = (int)nd;
    *work = (int*)malloc(sizeof(int) * (n + 1));
    if (!*work) {
      status = 1;
      return status;
    }
    *work[0] = n;
    int* ymap = *work + 1;
    for (int i = 0; i < nc; ++i) {
      int ci = (int)c[i];
      while(ci--)
        *ymap++ = i;
    }
  }
  int n = *work[0];
  int* ymap = *work + 1;
  for (int i=n-1; i>0; --i) {
    int j = random_interval(rstate, i);
    int tmp = ymap[j];
    ymap[j] = ymap[i];
    ymap[i] = tmp;
  }
  for (int i = 0, nrc = (nr * nc); i < nrc; ++i)
    matrix[i] = 0;
  for (int ir = 0; ir < nr; ++ir) {
    int ri = (int)r[ir];
    while(ri--)
      *ptr(matrix, nr, nc, ir, *ymap++) += 1;
  }
  return 0;
}
int rcont1 ( double * matrix , int nr , const double * r , int nc , const double * c , int * * work , bitgen_t * rstate ) { int status = 0 ; if ( * work == 0 ) { double nd = 0 ; status = rcont_check ( & nd , matrix , nr , r , nc , c ) ; if ( status != 0 ) return status ; int n = ( int ) nd ; * work = ( int * ) malloc ( sizeof ( int ) * ( n + 1 ) ) ; if ( ! * work ) { status = 1 ; return status ; } * work [ 0 ] = n ; int * ymap = * work + 1 ; for ( int i = 0 ; i < nc ; ++ i ) { int ci = ( int ) c [ i ] ; while ( ci -- ) * ymap ++ = i ; } } int n = * work [ 0 ] ; int * ymap = * work + 1 ; for ( int i = n - 1 ; i > 0 ; -- i ) { int j = random_interval ( rstate , i ) ; int tmp = ymap [ j ] ; ymap [ j ] = ymap [ i ] ; ymap [ i ] = tmp ; } for ( int i = 0 , nrc = ( nr * nc ) ; i < nrc ; ++ i ) matrix [ i ] = 0 ; for ( int ir = 0 ; ir < nr ; ++ ir ) { int ri = ( int ) r [ ir ] ; while ( ri -- ) * ptr ( matrix , nr , nc , ir , * ymap ++ ) += 1 ; } return 0 ; }
Generate random two-way table with given marginal totals.
Generate random two - way table with given marginal totals .
// shuffle ymap // clear table // fill table