/* nag_rngs_gen_multinomial (g05mrc) Example Program. * * Copyright 2001 Numerical Algorithms Group. * * Mark 7, 2001. */ #include #include #include #include int main(void) { /* Scalars */ Integer i, igen, j, k, m, n, nr; Integer exit_status = 0; Integer pdx; NagError fail; Nag_OrderType order; /* Arrays */ double *p = 0, *r = 0; Integer *x = 0; Integer iseed[4]; #ifdef NAG_COLUMN_MAJOR #define X(I, J) x[(J-1)*pdx + I - 1] order = Nag_ColMajor; #else #define X(I, J) x[(I-1)*pdx + J - 1] order = Nag_RowMajor; #endif INIT_FAIL(fail); printf( "nag_rngs_gen_multinomial (g05mrc) Example Program Results\n\n"); k = 4; n = 20; nr = 16007; /* Allocate memory */ if (!(p = NAG_ALLOC(k, double)) || !(r = NAG_ALLOC(nr, double)) || !(x = NAG_ALLOC(n * k, Integer))) { printf("Allocation failure\n"); exit_status = -1; goto END; } #ifdef NAG_COLUMN_MAJOR pdx = n; #else pdx = k; #endif /* Set the distribution parameters P and M */ p[0] = 0.08; p[1] = 0.1; p[2] = 0.8; p[3] = 0.02; m = 6000; /* Initialise the seed to a repeatable sequence */ iseed[0] = 1762543; iseed[1] = 9324783; iseed[2] = 42344; iseed[3] = 742355; /* igen identifies the stream. */ igen = 1; /* nag_rngs_init_repeatable (g05kbc). * Initialize seeds of a given generator for random number * generating functions (that pass seeds explicitly) to give * a repeatable sequence */ nag_rngs_init_repeatable(&igen, iseed); /* Choose MODE = 2 */ /* nag_rngs_gen_multinomial (g05mrc). * Generates a vector of random integers from a multinomial * distribution, seeds and generator number passed * explicitly */ nag_rngs_gen_multinomial(order, 2, m, k, p, n, x, pdx, igen, iseed, r, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_rngs_gen_multinomial (g05mrc).\n%s\n", fail.message); exit_status = 1; goto END; } for (i = 1; i <= n; ++i) { for (j = 1; j <= k; ++j) { printf("%12ld%s", X(i, j), j%10 == 0 || j == 4?"\n":" "); } } END: if (p) NAG_FREE(p); if (r) NAG_FREE(r); if (x) NAG_FREE(x); return exit_status; }