home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume9 / rndname3 / name.c < prev    next >
C/C++ Source or Header  |  1989-11-08  |  4KB  |  115 lines

  1. /**************************************************************************
  2.  *      name - prints random names                                        * 
  3.  *  Copyright 1988, 1989 by Brett Slocum.  All rights reserved.           *
  4.  *  Permission is granted to distribute, modify, or use portions of this  *
  5.  *  code in other programs, as long as this notice remains intact.        *
  6.  *  This program or its derivatives may not be sold for profit without    *
  7.  *  permission.                                                           *
  8.  *                                                                        *
  9.  *  UNIX  Version: Brett Slocum UUCP: ...uunet!hi-csc!slocum              *
  10.  *                              ARPA: slocum@hi-csc.honeywell.com         *
  11.  *  Changes were suggested by Geoff Kimbrough <geoff@ism780c.isc.com>     *
  12.  *                                                                        *
  13.  *  IBM Microsoft C v5.0 changes by Loyd Blankenship on 11-15-88          *
  14.  *                            UUCP: ...rutgers.edu!cs.utexas.edu!nth!loyd *
  15.  *                                                                        *
  16.  *  Version 2 by Brett Slocum 3/22/89 : various cleanups, features, etc.  *
  17.  *                                                                        *
  18.  *  Version 3 by Brett Slocum 11/7/89 : Scrabble (tm) letter distribution *
  19.  *                                                                        *
  20.  **************************************************************************/
  21.  
  22. #ifdef SYSV
  23. #include <string.h>
  24. #define srandom srand48
  25. #define random lrand48
  26. #endif
  27.  
  28. #ifdef BSD
  29. #include <strings.h>
  30. #endif
  31.  
  32. #ifdef MSC
  33. #include <string.h>
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <time.h>
  37. #define srandom srand
  38. #define random rand
  39. #endif
  40.  
  41. #include <ctype.h>
  42.  
  43. #define YES 1
  44. #define NO 0
  45. #define MAXNAME 30
  46. #define MINLENGTH 3
  47. #define RANGE 6
  48. #define rnd(n) (unsigned)(random() % (n))
  49.  
  50. /*
  51.    Since "y" appears twice, once in each string; all other 
  52.    letters, except "'", are doubled to keep the distribution even.
  53.    By adding or deleting the letters from these strings, you
  54.    can change the frequency of appearance of the letters,
  55.    (i.e. by adding an "e" and a "t" and deleting a "z" and a "q",
  56.    a distribution closer to English could be made.)
  57.  
  58.    Another set of strings has the Scrabble (tm) distributions of letters.
  59.    Put -DSCRABBLE into the Makefile, if you want the Scrabble (tm) distribution.
  60. */
  61.  
  62. #define FLAT_VOWELS "aaeeiioouuy'"
  63. #define FLAT_CONS "bbccddffgghhjjkkllmmnnppqqrrssttvvwwxxyzz'"
  64. #define SCRABBLE_VOWELS "aaaaaaaaaeeeeeeeeeeeeiiiiiiiiioooooooouuuuy'"
  65. #define SCRABBLE_CONS "bbccddddffggghhjkllllmmnnnnnnppqrrrrrrssssttttttvvwwxyz'"
  66.  
  67. #ifdef SCRABBLE
  68. char vowels[] = SCRABBLE_VOWELS;
  69. char cons[] = SCRABBLE_CONS;
  70. #else
  71. char vowels[] = FLAT_VOWELS;
  72. char cons[] = FLAT_CONS;
  73. #endif
  74.  
  75. main(argc, argv)  
  76. int argc;
  77. char *argv[];
  78. {
  79.      int n, letters, vowel;
  80.      char name[MAXNAME];
  81.  
  82.      if (argc == 2) {
  83.          /* initialize random number generator */
  84.          srandom(time(0L));
  85.          
  86.          /* generate argv[1] names */
  87.          for (n = atoi(argv[1]); n > 0; n--) {
  88.              name[0] = '\0';
  89.              /* choose whether first character is a vowel */
  90.              vowel =  (rnd(2) ? YES : NO);
  91.              /* get initial character - not " ' " */
  92.              if (vowel)  
  93.                  strncat(name, &vowels[rnd(sizeof(vowels) - 2)], 1);
  94.              else 
  95.                  strncat(name, &cons[rnd(sizeof(cons) - 2)], 1);
  96.              /* upper case first letter */
  97.              name[0] = _toupper(name[0]);
  98.              /* complete rest of letters */
  99.              for (letters = rnd(RANGE) + MINLENGTH; letters > 0; letters--) {
  100.                  /* alternate vowels and consonants */
  101.                  vowel = ( vowel ? NO : YES );
  102.                  /* get next character */
  103.                  if (vowel) 
  104.                      strncat(name, &vowels[rnd(sizeof(vowels)-1)], 1);
  105.                  else 
  106.                      strncat(name, &cons[rnd(sizeof(cons)-1)], 1);
  107.              }
  108.              printf("%s\n", name);
  109.          }
  110.      }
  111.      else 
  112.          printf("Usage: name number-of-names\n");
  113. }
  114.  
  115.