[alt.sources] LOTTO computer easy-pick lottery numbers generator

norstar@tnl.UUCP (Daniel Ray) (01/04/91)

Below is a quick-hack lotto program, for use in the widely popular Keno-based
state and provincial lotteries. Remember to set the two #define statements to
reflect the popular game in your locality. Defaults can be overridden on the
command line. Can be used either for Lotto or Keno. The man page follows the
source code. Presumes that you have the drand48() C function.

Enjoy.

dan ray
admin the northern lights
(currently offline, in transit to a new place and a new life)
P.S. DONT SEND ME EMAIL. BY THIS SUNDAY JAN 6, TNL WILL BE OFF THE AIR FOR
A FEW MONTHS WHILE I MOVE BACK TO THE U.S. AND LOOK FOR ANOTHER JOB.

---------------------------------couper ici----------------------------------
/*
 *   LOTTO - Keno-type lottery numbers generator, v1.0 4-January-1991
 *   by Daniel Ray, sysop of The Northern Lights (previously of Montreal, PQ)
 *
 *   THIS PROGRAM IS RELEASED INTO THE PUBLIC DOMAIN. NO DISTRIBUTION
 *   RESTRICTIONS EXIST WHATSOEVER.
 *
 *   All I ask is that.... if you strike it rich, remember me. You can send
 *   anything you consider appropriate to:
 *
 *   Daniel Ray
 *   C/O Denison Ray
 *   P.O. Box 255
 *   North Chatham, NY 12132
 *
 *   Usage: lotto		(uses defaults, see below)
 *          lotto 60            (change highest number to 60, same no. choices)
 *          lotto 80 10         (chooses 10 numbers from 1-80 for Keno)
 *
 *   Method: random number generator uses drand48(). The srand48() seeder uses
 *   the epoch time plus the previously found lotto no. (which starts as 13).
 *   If there is any magic in this, it is borrowed from the power of the
 *   number thirteen. Really a splendidly lucky number, its meaning has
 *   become perverted in Western culture to be "unlucky".
 *
 *   TO COMPILE:   cc -O lotto.c -o lotto
 */

/* Attention: define the below two numbers to match your state or province ** */

#define HIGHESTNUMBER  49
#define SELECTIONS     6    /* May want to make this 7 for 'bonus no.' lottos */

/* End of configuration section *****Leave the rest alone for luck*********** */


#include <stdio.h>

main(argc,argv)
int argc;
char **argv;
{
int		highest, numbers, loops=0, cloops=0, badchoice=0;
extern long	lrand48();
extern void	srand48();
extern long	time();
long		choice=13;		/* 13 starting seed for good luck! */
long		oldchoice[100];
	
	switch(argc) {
		case 1: highest = HIGHESTNUMBER;
			numbers = SELECTIONS;
			break;
		case 2: highest = atoi(argv[1]);
			numbers = SELECTIONS;
			break;
		case 3: highest = atoi(argv[1]);
			numbers = atoi(argv[2]);
			break;
	}
	
	/* CHECK FOR ERRORS */
	if((highest <= 0)||(numbers <= 0)){
		fprintf(stderr,"Usage: %s [highnumber] [selections]     Lotto generator program\n",argv[0]);
		exit(2);
	}
	if(highest<=numbers){
		fprintf(stderr,"%s: highnumber(%d) must be greater than %d\n",argv[0],highest,numbers);
		exit(1);
	}

	/* DRAND48() RANDOM NUMBER LOOP */
	oldchoice[0] = 0;
	while(loops < numbers){
		srand48(time((long *)0)+choice);
		choice = lrand48() % highest + 1;
		
		/* CHECK FOR DUPLICATIONS */
		while(loops > cloops){
			if(oldchoice[cloops] == choice){
				badchoice = 1;
			}
			cloops++;
		}
		if(badchoice != 1){
			oldchoice[loops] = choice;
			printf("%ld ",choice);
			loops++;
		} else {
			badchoice = 0;
		}
		cloops = 0;
	}
	
	/* FINISH PROGRAM, PRINT SELECTION LIMITS */
	printf("   [%d/%d]\n",numbers,highest);
	exit(0);
}
-----------------------------end of lotto.c; lotto.1 man page follows---------


LOTTO(1)		The Northern Lights			LOTTO(1)


Name
	lotto - Lottery numbers generator, for use in state/provincial lottos


Syntax
	lotto [highestnumber] [selections]


Description
	Lotto is a random number generator that provides possibly winning
	choices to lotto-type state or provincial lotteries. This usually
	consists of SIX different numbers between 1 and a HIGH NUMBER. The
	lottery and state or province sets the upper number, which thus
	varies depending on where you are. Lotto defaults to '49' as the
	high number, and '6' as the number of selections. You can override
	these defaults on the command line.

	The basis of "lotto" lotteries is the Las Vegas game of Keno. The
	'lotto' program can also be used to play Keno.


Method
	Lotto uses the drand48() function to generate random numbers, using
	the epoch time plus the previous random number as the seed for each
	round in the loop. Lotto will execute until it finds the necessary
	number of different values.


Errors
	The high number must be at least one more than the number of selec-
	tions, or an error message will be returned. Only nonzero integers
	less than 32676 can be entered on the command line.


LOTTO(1)		The Northern Lights			LOTTO(1)

-------------------------------thats all folks!-----------------------------

Michael Barnett (01/07/91)

norstar@tnl.UUCP (Daniel Ray) writes:


 >source code. Presumes that you have the drand48() C function.
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 >Enjoy.

I'd like to, but where do I find the drand48() C function, or is that a dumb
question?
-- 
          Michael Barnett s873561@minyos.xx.rmit.oz.au
                 RMIT/VUT, Melbourne, Australia
   "The opinions expressed here belong to whoever wants them" :-)  

jpr@jpradley.jpr.com (Jean-Pierre Radley) (01/13/91)

In article <7457@minyos.xx.rmit.oz.au> Michael Barnett writes:
>norstar@tnl.UUCP (Daniel Ray) writes:
>
>
> >source code. Presumes that you have the drand48() C function.
>
>I'd like to, but where do I find the drand48() C function, or is that a dumb
>question?


It's been part of the C library on all the *nix that I've run for the last
eight years.
-- 

 Jean-Pierre Radley	    NYC Public Unix	jpr@jpr.com	CIS: 72160,1341

kevin@ttidca.TTI.COM (Kevin Carothers) (01/15/91)

In article <1991Jan12.171423.5842@jpradley.jpr.com> jpr@jpradley.UUCP (Jean-Pierre Radley) writes:
%In article <7457@minyos.xx.rmit.oz.au> Michael Barnett writes:
%>norstar@tnl.UUCP (Daniel Ray) writes:
%>
%>
%> >source code. Presumes that you have the drand48() C function.
%>
%>I'd like to, but where do I find the drand48() C function, or is that a dumb
%>question?
%
%
%It's been part of the C library on all the *nix that I've run for the last
%eight years.

 this lotto generator didn't make it here. 
 Here's one I cooked up. I have  my own random generator 
 that I snarfed out of D. Knuth Vol 3 (remember that one ? :-)
 which seems to be "random enough".

 Just whack at it a little to tailor it to the game of your choice.
 Wrote it on a SUN. Should be fairly transportable (no serious lint
 errors).
--------------------------------------------------------------------
#include <stdio.h>
#include <sys/types.h>
#include <sys/timeb.h>


#define xrand(x)  ((x * 1103515245 + 12345) & 0x7fffffff) 
#define SIZE (sizeof nums / sizeof(nums[0]))
int Cnt = 0;
main(argc,argv)
int argc;
char *argv[];
{

int j, i, k, seed;
struct timeb *tp;
long time();
int nums[6];
int tnbr;
extern int errno;

    for(k=0; k<((argc==2)?argc:1); k++) {
	seed = (int) time(&tp);
	printf("Lucky lotto number is: ");

	for (i=0; i < SIZE; i++) {
	    *(nums+i) = 0;
	    grand(&seed,&nums[0], i);
	    }

	for(i=0; i < SIZE; i++) 
	    for(j=0; j < i; j++) 
		if(nums[j]>nums[i]) {
		    tnbr = nums[j];
		    nums[j] = nums[i];
		    nums[i] = tnbr;
		}

	for(i=0; i < SIZE; i++) {
		printf("%d%s",nums[i],((i<5)?"-":" "));
		nums[i] = 0;
	}
	printf("\n");
    }
}

grand(seed, pnbr, start)
int *seed;
int *pnbr;
int start;
{
	int i, j, nbr;
   	nbr = 0;

        while (!nbr) {
	    nbr = *seed = (int) (xrand(*seed) % 54);
	    }

	for(j=0; j<=start; j++) {
	    if ((nbr == *(pnbr+j)) && (*(pnbr+j) != 0)){ /* No duplicates */
		*seed +=  (int) xrand(*seed);
		grand(seed,pnbr,start);
		}
	    }
	    *(pnbr+start) = nbr;
}