NAG Library Manual, Mark 27.2
Interfaces:  FL   CL   CPP   AD 

NAG CL Interface Introduction
Example description
/* nag_rand_sample (g05ndc) Example Program.
 *
 * Copyright 2021 Numerical Algorithms Group.
 *
 * Mark 27.2, 2021.
 */
/* Pre-processor includes */
#include <math.h>
#include <nag.h>
#include <stdio.h>

int main(void) {
  /* Integer scalar and array declarations */
  Integer exit_status = 0;
  Integer i, lstate, m;
  Integer *ipop = 0, *isampl = 0, *state = 0;

  /* NAG structures */
  NagError fail;

  /* Population size */
  Integer n = 8;

  /* Choose the base generator */
  Nag_BaseRNG genid = Nag_Basic;
  Integer subid = 0;

  /* Set the seed */
  Integer seed[] = {1762543};
  Integer lseed = 1;

  /* Initialize the error structure */
  INIT_FAIL(fail);

  printf("nag_rand_sample (g05ndc) Example Program Results\n\n");

  /* Get the length of the state array */
  lstate = -1;
  nag_rand_init_repeat(genid, subid, seed, lseed, state, &lstate, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_rand_init_repeat (g05kfc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  if (!(ipop = NAG_ALLOC(n, Integer)) || !(isampl = NAG_ALLOC(n, Integer)) ||
      !(state = NAG_ALLOC(lstate, Integer))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Initialize the generator to a repeatable sequence */
  nag_rand_init_repeat(genid, subid, seed, lseed, state, &lstate, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_rand_init_repeat (g05kfc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  printf("  Samples from the first %1" NAG_IFMT " integers\n", n);
  printf("  Sample size     Values\n");

  /* Initialize the population */
  for (i = 0; i < n; i++)
    ipop[i] = i + 1;

  /* Generate samples of different sizes */
  for (m = 1; m <= n; m++) {
    nag_rand_sample(ipop, n, isampl, m, state, &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_rand_sample (g05ndc).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }

    /* Display the results */
    printf(" %6" NAG_IFMT "           ", m);
    for (i = 0; i < m; i++)
      printf("%2" NAG_IFMT "%s", isampl[i], (i + 1) % 8 ? " " : "\n");
    if (m % 8)
      printf("\n");
  }

END:
  NAG_FREE(ipop);
  NAG_FREE(isampl);
  NAG_FREE(state);

  return exit_status;
}