[comp.sources.misc] v05i039: Random Name Generator Source Code

slocum@hi-csc.UUCP (Brett Slocum) (11/09/88)

Posting-number: Volume 5, Issue 39
Submitted-by: "Brett Slocum" <slocum@hi-csc.UUCP>
Archive-name: rnameg

[Looks reasonably portable to BSD or System V, but wants one of the better
random number generators.  ++bsa]

This program is a very simple random name generator that produces
surprisingly good names.  They tend to be exotic, since 'X' and 
'Z' have the same chance of coming up as 'T' and 'N'.  Many of the
names have a Japanese or African flavor.  Some of my favorites
came in the first page: Zozaras, Lyxam, Rux, and Zelica.

The program 'name.c' produces the names, and two scripts are 
included to formats them into pages: names.csh and names.sh, 
for C-shell and Bourne shell, respectively.

These programs have been tested on an Apollo DN330 with both
BSD4.2 and Sys V, and on BSD4.3.  The makefile and names.csh 
script have Sys V versions commented out.

Send any changes or updates to me:

Brett Slocum
...uunet!hi-csc!slocum
slocum@hi-csc.honeywell.com

P.S. Thanks to Geoff Kimbrough <geoff@ism780c.isc.com> for
suggesting some improvements.

#--------------------------------CUT HERE-------------------------------------
#! /bin/sh
#
# This is a shell archive.  Save this into a file, edit it
# and delete all lines above this comment.  Then give this
# file to sh by executing the command "sh file".  The files
# will be extracted into the current directory owned by
# you with default permissions.
#
# The files contained herein are:
#
# \c
-rwxrwx---  1 slocum        251 Nov  2 09:15 Makefile
# \c
-rwxrwx---  1 slocum        451 Nov  2 09:15 names.csh
# \c
-rwxrwx---  1 slocum        111 Nov  2 09:15 names.sh
# \c
-rwxrwx---  1 slocum       2545 Nov  2 09:15 name.c
#
echo 'x - Makefile'
if test -f Makefile; then echo 'shar: not overwriting Makefile'; else
sed 's/^X//' << '________This_Is_The_END________' > Makefile
X#  makefile for name.c
X#
X#       Comment out the BSD line if you're on System V
X#       Comment out the SYSV line if you're using Berkely Unix.
X 
X# SYSTEM = -DSYSV 
XSYSTEM = -DBSD
X 
XCFLAGS = -O $(SYSTEM)
X 
Xname:   name.c
X	cc -o name $(CFLAGS) name.c
X
________This_Is_The_END________
if test `wc -l < Makefile` -ne 13; then
	echo 'shar: Makefile was damaged during transit (should have been 13 bytes)'
fi
fi		; : end of overwriting check
echo 'x - names.csh'
if test -f names.csh; then echo 'shar: not overwriting names.csh'; else
sed 's/^X//' << '________This_Is_The_END________' > names.csh
X#! /bin/csh
X# names - generate random names
X#
X# usage: names [pages]
X
X# bsd script -- uncomment these lines if bsd
X
Xif ($#argv == 0) then
X    name 336 | sort -u | pr -6 | expand
Xelse
X    @ n = 336 * $1
X    name $n | sort -u | pr -6 | expand
Xendif
X
X# sysv script -- uncomment these lines if sysv
X# note: no tab expansion performed
X
X#if ($#argv == 0) then
X#    name 336 | sort -u | pr -6
X#else
X#    @ n = 336 * $1
X#    name $n | sort -u | pr -6
X#endif
X
________This_Is_The_END________
if test `wc -l < names.csh` -ne 24; then
	echo 'shar: names.csh was damaged during transit (should have been 24 bytes)'
fi
fi		; : end of overwriting check
echo 'x - names.sh'
if test -f names.sh; then echo 'shar: not overwriting names.sh'; else
sed 's/^X//' << '________This_Is_The_END________' > names.sh
Xif [ $# = 0 ]
Xthen
X    name 336 | sort -u | pr -6
Xelse
X    n=`expr 336 \* $1`
X    name $n | sort -u | pr -6
Xfi
________This_Is_The_END________
if test `wc -l < names.sh` -ne 7; then
	echo 'shar: names.sh was damaged during transit (should have been 7 bytes)'
fi
fi		; : end of overwriting check
echo 'x - name.c'
if test -f name.c; then echo 'shar: not overwriting name.c'; else
sed 's/^X//' << '________This_Is_The_END________' > name.c
X/**************************************************************************
X *      name - prints random names                                        * 
X *  Copyright 1988 by Brett Slocum.  All rights reserved.                 *
X *  Permission is granted to distribute, modify, or use portions of this  *
X *  code in other programs, as long as this copyright notice remains      *
X *  intact.  This program may not be sold for profit without permission.  *
X *                                                                        *
X *  Changes were suggested by Geoff Kimbrough <geoff@ism780c.isc.com>     *
X **************************************************************************/
X
X#ifdef SYSV
X#include <string.h>
X#define srandom srand48
X#define random lrand48
X#else
X#include <strings.h>
X#endif
X
X#include <ctype.h>
X
X#define YES 1
X#define NO 0
X#define MAXNAME 30
X#define MINLENGTH 3
X#define RANGE 6
X#define rnd(n) ((unsigned) random() % (unsigned) n)
X
X/* Since "y" and "'" appear twice, once in each array, all other 
X   letters are doubled to keep the distribution even. */
X
X char vowels[] = "aaeeiioouuy'";
X char cons[] = "bbccddffgghhjjkkllmmnnppqqrrssttvvwwxxyzz'";
X
Xmain(argc, argv)  
Xint argc;
Xchar *argv[];
X{
X     int n, letters, vowel;
X     char name[MAXNAME];
X
X     if (argc == 2) {
X         /* initialize random number generator */
X         srandom(time(0L));
X
X         /* generate argv[1] names */
X         for (n = atoi(argv[1]); n > 0; n--) {
X             name[0] = '\0';
X             /* choose whether first character is a vowel */
X             vowel =  (rnd(2) ? YES : NO);
X             /* get initial character - not " ' " */
X             if (vowel)  
X                 strncat(name, &vowels[rnd(sizeof(vowels)-2)], 1);
X             else 
X                 strncat(name, &cons[rnd(sizeof(cons)-2)], 1);
X             /* upper case first letter */
X             name[0] = _toupper(name[0]);
X             /* complete rest of letters */
X             for (letters = rnd(RANGE) + MINLENGTH; letters > 0; letters--) {
X                 /* alternate vowels and consonants */
X                 vowel = ( vowel ? NO : YES );
X                 /* get next character */
X                 if (vowel) 
X                     strncat(name, &vowels[rnd(sizeof(vowels))], 1);
X                 else 
X                     strncat(name, &cons[rnd(sizeof(cons))], 1);
X             }
X             printf("%s\n", name);
X         }
X     }
X     else 
X         usage();
X}
X
Xusage()  /* print usage statement */
X{
X     printf("Usage: name number-of-names\n");
X}
________This_Is_The_END________
if test `wc -l < name.c` -ne 77; then
	echo 'shar: name.c was damaged during transit (should have been 77 bytes)'
fi
fi		; : end of overwriting check
exit 0
-- 
Brett Slocum   UUCP: ...uunet!hi-csc!slocum
               Arpa: slocum@hi-csc.honeywell.com
"My name is Inigo Montoya. You killed my father. Prepare to die."