home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / graphics-0.17 / dist-stat / rand.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-13  |  4.7 KB  |  223 lines

  1. /*
  2.  * $Header: /files1/home/toy/src/stat/RCS/rand.c,v 1.6 90/09/10 12:24:04 toy Exp $
  3.  * NAME
  4.  *    rand - generate a random sequence
  5.  *
  6.  * SYNOPSIS
  7.  *    rand [-H] [-P prec] [-c cols] [-h high] [-l low] [-m mult] [-n num] [-s seed]
  8.  *
  9.  * DESCRIPTION
  10.  *    Output is a set of random values.
  11.  *
  12.  * HISTORY
  13.  * $Log:    rand.c,v $
  14.  * Revision 1.6  90/09/10  12:24:04  toy
  15.  * Some help strings didn't have a comma in them.
  16.  *
  17.  * Revision 1.5  90/09/04  17:40:44  toy
  18.  * Set default precision from the environment variable STAT_PREC.
  19.  * Use print_help_strings to print out help.
  20.  *
  21.  * Revision 1.4  90/09/02  17:12:28  toy
  22.  * Fixed revision history
  23.  *
  24.  * Revision 1.3  90/09/01  17:02:24  toy
  25.  * Use message function to print out messages.
  26.  *
  27.  * Revision 1.2  90/09/01  12:56:38  toy
  28.  * Wasn't handling -n option correctly.
  29.  *
  30.  * Revision 1.1  90/09/01  12:54:24  toy
  31.  * Initial revision
  32.  */
  33.  
  34. #include <stdio.h>
  35.  
  36. #if    defined(__STDC__) || defined(__GNUC__)
  37. #include <stddef.h>
  38. #include <stdlib.h>
  39. #endif
  40.  
  41. #include "gps.h"
  42.  
  43. #define    DEF_PROGNAME    "rand"
  44.  
  45. static const char RCSID[] = "@(#) $Id: rand.c,v 1.6 90/09/10 12:24:04 toy Exp $";
  46.  
  47. extern int getopt ();
  48. extern int optind;
  49. extern char *optarg;
  50.  
  51. extern long random PARMS ((void));
  52. extern void srandom PARMS ((unsigned));
  53.  
  54. const char *progname;
  55.  
  56. #define    OPT_STRING    "HP:c:h:l:m:n:s:"
  57.  
  58. void
  59. help ()
  60. {
  61.   static const char *help_strings[] =
  62.   {
  63.     "\t-H\tThis help.\n",
  64.     "\t-P p\tSet number of significant digits to p.  If not specified\n",
  65.     "\t\tuse value of STAT_PREC from environment, if available.  Else\n",
  66.     "\t\tuse default.\n",
  67.     "\t-c c\tSet number of output elements per line to n.\n",
  68.     "\t-h h\tHighest number is h.  Default is 1.\n",
  69.     "\t-l l\tLowest number is l.  Default is 0.\n",
  70.     "\t-m m\tMultiplier is m.  Default is determined from h and l.\n",
  71.     "\t\tCannot be specified with either -h or -l.\n",
  72.     "\t-n n\tNumber of values to generate.\n",
  73.     "\t-s s\tSet seed to s.  Default is 1.\n",
  74.     "Generate a set of uniformly distributed random numbers with\n",
  75.     "the given range.  The default range is [0, 1).  If a multiplier\n",
  76.     "is given, the numbers are multiplied appropriately.\n",
  77.     NULL
  78.   };
  79.   (void) fprintf (stderr, "%s\n", RCSID);
  80.   (void) fprintf (stderr, "Usage:  %s [-H] [-P prec] [-c cols] [-h high] [-l low]\n", progname);
  81.   (void) fprintf (stderr, "\t\t[-m mult] [-n num] [-s seed]\n");
  82.   print_help_strings (help_strings);
  83. }
  84.  
  85.  
  86. int
  87. main (argc, argv)
  88.      int argc;
  89.      char *argv[];
  90. {
  91.   int option;
  92.   int errcnt;
  93.   int got_high;
  94.   int got_low;
  95.   int got_mult;
  96.   int num;
  97.   unsigned seed;
  98.   double low;
  99.   double high;
  100.   double mult;
  101.   double x;
  102.  
  103.   progname = get_my_name (argv[0], DEF_PROGNAME);
  104.   set_def_prec ();
  105.  
  106.   /*
  107.    * Read options
  108.    */
  109.  
  110.   errcnt = 0;
  111.   got_high = got_low = got_mult = FALSE;
  112.   low = 0;
  113.   high = 1;
  114.   num = 10;
  115.   seed = 1;
  116.  
  117.   while ((option = getopt (argc, argv, OPT_STRING)) != -1)
  118.     {
  119.       switch (option)
  120.     {
  121.     case 'H':        /* Help                 */
  122.       help ();
  123.       exit (0);
  124.       break;
  125.     case 'P':        /* Precision             */
  126.       (void) set_precision (atoi (optarg));
  127.       break;
  128.     case 'c':        /* Number of columns         */
  129.       (void) set_max_columns (atoi (optarg));
  130.       break;
  131.     case 'h':        /* High             */
  132.       if (sscanf (optarg, "%lf", &high) != 1)
  133.         {
  134.           message ("cannot read -h value: `%s'\n", optarg);
  135.           errcnt++;
  136.         }
  137.       else
  138.         {
  139.           got_high = TRUE;
  140.         }
  141.       break;
  142.     case 'l':        /* Low             */
  143.       if (sscanf (optarg, "%lf", &low) != 1)
  144.         {
  145.           message ("cannot read -l value: `%s'\n", optarg);
  146.           errcnt++;
  147.         }
  148.       else
  149.         {
  150.           got_low = TRUE;
  151.         }
  152.       break;
  153.     case 'm':        /* Multiplier         */
  154.       if (sscanf (optarg, "%lf", &mult) != 1)
  155.         {
  156.           message ("cannot read -m value: `%s'\n", optarg);
  157.           errcnt++;
  158.         }
  159.       else
  160.         {
  161.           got_mult = TRUE;
  162.         }
  163.       break;
  164.     case 'n':        /* Number of samples     */
  165.       if (sscanf (optarg, "%d", &num) != 1)
  166.         {
  167.           message ("cannot read -n value: `%s'\n", optarg);
  168.           errcnt++;
  169.         }
  170.       break;
  171.     case 's':        /* Seed             */
  172.       if (sscanf (optarg, "%u", &seed) != 1)
  173.         {
  174.           message ("cannot read -s value: `%s'\n", optarg);
  175.           errcnt++;
  176.         }
  177.       else
  178.         {
  179.           srandom (seed);
  180.         }
  181.       break;
  182.     default:
  183.       break;
  184.     }            /* endswitch     */
  185.     }                /* endwhile     */
  186.  
  187.   /*
  188.    * Check options
  189.    */
  190.  
  191.   if (got_mult && (got_low || got_high))
  192.     {
  193.       message ("cannot specify limits with a multiplier\n");
  194.       errcnt++;
  195.     }                /* endif     */
  196.  
  197.   /*
  198.    * Exit if any errors found
  199.    */
  200.   if (errcnt > 0)
  201.     {
  202.       exit (1);
  203.     }
  204.   if (!got_mult)
  205.     {
  206.       mult = high - low;
  207.     }
  208.  
  209.   /*
  210.    * Generate the desired random numbers
  211.    */
  212.  
  213.   while (num-- > 0)
  214.     {
  215.       x = random () / 2147483648.0;    /* 2**31 = 2147483648     */
  216.       x = low + mult * x;
  217.       print_number (stdout, x);
  218.     }
  219.  
  220.   end_column (stdout);
  221.   return 0;
  222. }
  223.