home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / mrandom / mrtest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-12  |  2.6 KB  |  105 lines

  1. /*
  2.  *        mrtest.c
  3.  *
  4.  *    Test routine for mrandom.c 
  5.  *
  6.  *    Original Implementation:
  7.  *        Clark Thomborson, September 1991
  8.  *
  9.  *    This material is based upon work supported by the National
  10.  *    Science Foundation under grant number MIP-9023238.  The
  11.  *    Government has certain rights in this material.
  12.  *
  13.  *    Any opinions, findings, and conclusions or recommendations
  14.  *    expressed in this material are those of the author and do
  15.  *    not necessarily reflect the view of the National Science
  16.  *    Foundation.
  17.  *
  18.  *    This code is neither copyrighted nor patented.
  19.  */
  20.  
  21. # include <sys/file.h> /* we use access() */
  22. # include "mrandom.h"
  23.  
  24. # define RNGFILENAME "RNGstatefile" /* where the RNG state is stored */
  25.  
  26. /* calling conventions */
  27. void argerr(progname)
  28. char *progname;
  29. {
  30.   printf("Usage: %s nn -snnnnnnnn -mnn R\n", progname);
  31.   printf("  nn sets number of random generates\n");
  32.   printf("  -snnnnnnnn seeds the RNG with the given value\n");
  33.   printf("  -mnnnn adjusts the range of the RNG\n");
  34.   exit(1);
  35. } /* end argerr */
  36.  
  37. /* A simple test-driver */
  38. int main(argc,argv)
  39. int argc; char *argv[];
  40. {
  41.   /* command-line arguments, with default values */
  42.   int reseeding=0; /* nonzero if RNG state will be re-initialized */
  43.   int seed=1573122697; /* new seed for RNG */
  44.   int n=10; /* number of randoms to generate */
  45.   int m=100; /* desired range of random outputs: 0..m-1 */
  46.  
  47.   int i; /* temp */
  48.   char rngdesc[RNGIDSTRLEN]; /* for a describe_rng() call */
  49.  
  50.   if(argc > 1) {
  51.     for(i=1;i<argc;i++) {
  52.       if(argv[i][0] >= '0' && argv[i][0] <= '9') {
  53.     n = atoi(&(argv[i][0]));
  54.       } else if(argv[i][0] == '-') {
  55.     switch(argv[i][1]) {
  56.     case 's': /* new seed for rng */
  57.       seed = atoi(&(argv[i][2]));
  58.       reseeding = 1;
  59.       break;
  60.     case 'm': /* adjust range of rng */
  61.       m = atoi(&(argv[i][2]));
  62.       break;
  63.     default:
  64.       argerr((char*) argv[0]);
  65.     }
  66.       } else {
  67.     argerr((char*) argv[0]);
  68.       }
  69.     }
  70.   }
  71.   
  72.   if (!reseeding ) {
  73.     if (access(RNGFILENAME, R_OK)) {
  74.       printf("There is no RNG statefile in this directory, so ");
  75.       printf("I'll make one for you.\n");
  76.       reseeding = 1;
  77.     }
  78.   }
  79.  
  80.   if (reseeding) { /* create a new statefile from scratch */
  81.     if (!init_rng(seed,RNGFILENAME)) {
  82.       exit(1);
  83.     }
  84.     printf("Initializing RNG.   ");
  85.   } else { /* use an existing statefile */
  86.     if (!restart_rng(RNGFILENAME)) {
  87.       exit(1);
  88.     }
  89.     printf("Restarting RNG.  ");
  90.   }
  91.   printf(describe_rng(rngdesc));
  92.  
  93.   printf("Here are %d random values in the range 0 to %d:\n", n, m-1);
  94.   for (i=0; i<n; i++) {
  95.     printf("  %d\n", mrandom(m));
  96.   }
  97.  
  98.   /* terminate job */
  99.   printf("Final ");
  100.   printf(describe_rng(rngdesc));
  101.  
  102.   return( save_rng(RNGFILENAME) );
  103.  
  104. } /* end main */
  105.