[net.sources] Lotto 6/49 number allocation program for office pools

dave@utcsrgv.UUCP (Dave Sherman) (01/10/84)

The staff in the office here are playing Loto 6/49 this week
(seems like everyone is), and they drafted me to have the
computer sort the numbers. Everyone who is playing put in
$1 and selected 6 numbers, but if there's a winner they don't
want anyone to be able to say "those are my numbers". So the
problem was to put ALL the numbers selected into a pot, and
to generate 6-number tickets from that pot.

Here's my half-hour hack for anyone who's interested. (The
getline() and yes() routines are from another package, which
is why they look more complex than they might have to be.)
Change /dev/console to whatever file you want to use to save
the tickets generated.


Dave Sherman
Toronto

========================================================================
/*
 * lotto - let people put their chosen lottery numbers into a
 * pot and generate tickets
 *
 *  Dave Sherman
 *  The Law Society of Upper Canada
 *  Toronto
 *  utcsrgv!lsuc!dave	   (also utcsrgv!dave at U of Toronto)
 */
#include <stdio.h>


int *a;
int array[180] = { 0 };
long now;
FILE *printer;
int ticket[6];
int totalnums;
char buffer[256];

main()
{
	register i, j;
	register sets;
	int try;

	printf("Hello.\n\n");
	printf("This is your friendly computer speaking.\n");
	printf("Today I'm going to help you win the lottery.\n");
	if(yes("\n\nDo you want to let me help you win Loto 6/49? ") == 0)
	{
		printf("Spoilsport!\n");
		exit(1);
	}

	printer = fopen("/dev/console", "w");
	if(printer == NULL)
	{
		printf("Sorry, I can't get the printer open.\n");
	}
	fprintf(printer, "\n   COMPUTER-SORTED LOTTERY NUMBERS\n\n");

	printf("\n");
	sleep(1);
  start:
	printf("\nOK, let's start.\n\nHow many sets of six numbers do you have?\n");
	getline(buffer);
	sets = atoi(buffer);
	if(yes("You have %d sets of numbers, then, is that right? ", sets) == 0)
	{
		printf("Oh. I must have misunderstood you.\n\n\n");
		goto start;
	}
	totalnums = sets * 6;

   next:
	printf("\nNow you have to tell me what the numbers are.\n\n");
	sleep(1);
	for(i=0; i<sets; i++)
	{
		printf("\n\nSET #%d:\n", i+1);
		printf("Type all six numbers, with a space after each one.\n");
		a = array+i*6;
		scanf("%d %d %d %d %d %d", a, a+1, a+2, a+3, a+4, a+5);
	}

	printf("\n\nThe numbers I have received are:\n\n");
	for(i=0; i<sets; i++)
	{
		for(j=0; j<6; j++)
			printf("%d   ", array[i*6+j]);
		printf("\n");
	}


	if(yes("\n\nIs this right? ") == 0)
	{
		printf("OK, let's try again.\n");
		goto next;
	}

	printf("OK. Now I'll give them to you in random order for the tickets.\n");

	time(&now);
	srand((int)now);

	for(i=0; i<sets; i++)
	{
		printf("\n\nTICKET #%d: ", i+1);
		zeroticket();
		for(j=0; j<6; )
		{
			try = rand() % totalnums;
			if(array[try] <= 0) continue;
			if(inticket(array[try])) continue;
			ticket[j++] = array[try];
			printf("  %d  ", array[try]);
			array[try] = 0;
		}
		printf("\n");
		fprintf(printer, "\n\nTICKET #%d: ", i+1);
		for(j=0; j<6; j++)
			fprintf(printer, "  %d  ", ticket[j]);
	}
	fprintf(printer, "\n\nHOPE YOU WIN!!!\n");
	putc(014, printer);
	fclose(printer);
	printf("\nThe numbers will be printed out on the printer for you,\nso you needn't write them down.\n");
	printf("\n\nHOPE YOU WIN!!!\n");
	exit(0);
}

inticket(n)
	register n;
{
	register k;
	for(k=0; k<6; k++)
		if(ticket[k] == n)
			return(1);
	return(0);
}

zeroticket()
{
	register k;
	for(k=0; k<6; k++)
		ticket[k] = 0;
}

getline(str)	/* put typed line into str, not including the newline */
	register char *str;
{
	register int c;
	register char *start;

	start = str;

     retry:
	alarm(3600);
	while(c = getchar())
	{
		/* EOF could be a genuine ctrl-d, but is more
		 * likely an error on the read caused by
		 * an interrupt signal being given from the
		 * terminal while we were waiting to read
		 */
		if(c == EOF)
		{
			*start = '\0';
			alarm(0);
			return(-1);
		}
		if(c == '\n')
		{
			*str = 0;
			if(*start == '!')
			{
				system(&start[1]);
				printf("!\n");
				str = start;
				goto retry;
			}
			alarm(0);
			return(0);
		}
		*str++ = c;
	}
}

yes(str,a,b)
	register char *str, *a;
	char *b;
{
	register char *p;
	p = buffer;
	while(1)
	{
		printf(str,a,b);
		getline(p);
		switch(*p)
		{
		case 'y':
		case 'Y':
			return(1);
		case 'n':
		case 'N':
			return(0);
		case 'm':
		case 'M':
			if(strncmp(p+1, "aybe", 4) == 0)
				printf("Very funny, aren't you?\nWe have ways of dealing with people like you.\n");
			goto deflt;
		case 'o':
		case 'O':
			if(*(p+1) == 'k' || *(p+1) == 'K')
				return(1);
			/* fall through */
		default:
		deflt:
			printf("Please answer yes or no.\n");
		}
	}
}
========================================================================
-- 
 {allegra,cornell,decvax,ihnp4,linus,utzoo}!utcsrgv!dave