Download Here:
http://dl.dropbox.com/u/52078/Development/C/lotto/lotto.c
——
I’ve been going though K&R C Programming Language lately & brushing up on my programming skills. All the while talking to a buddy of mine that programs frequently. He gave me a challenge to give him some lotto numbers for standard lotto & pick5. The final version is a pseudo random variation, but I don’t know enough programming know how yet to get it to be absolute, or even close to true random. Anywho about 30 minutes later I had a simple alteration whipped up like the following:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NUMBERS 6 /* How many numbers do you want? */
#define MAX 46 /* Max random value */
int main (void) {
int num;
/* get 6 random numbers */
srand ( time (NULL) ); /* make a random number based on unix time */
printf ("Your lotto numbers are: n");
for ( num = 0; num < NUMBERS; num++ ) { /* repeat for how many numbers you want */
if ( ( random = rand () % MAX ) == 0 ) /* prevents 0 from being outputted */
printf ( "t%d", rand () % MAX );
else
printf ( "t%d", random );
}
printf ("n");
return 0;
}
It will output something like the following:
Your lotto numbers are:
23 33 4 35 14 1
This will output 6 random numbers. The actual randomness is happening based on srand && it makes it random based on unix time. http://linux.die.net/man/3/srand has a nice little description of srand & rand, or if you’re at a terminal you can type ‘man srand’ and get some info.
After I finished this, low and behold he wanted pick5 too. (which is 5 sets of 6 numbers), well I added this & made a little prompt for ease of use. The following program is the complete solution. You can view it below or download it from my public dropbox link ^^ look at the top ^^:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NUMBERS 6 /* How many numbers do you want? */
#define MAX 46 /* Max random value */
int main (void) {
int num, pick5, type, random;
printf ( "Hey Chris, what would you like to do today?nn>> Type number you wish to execute:n" );
printf ( "(1) Standard Lotton" );
printf ( "(2) Pick5 Lotton" );
scanf ( "%d", &type );
switch ( type ) {
case 1: /* get 6 random numbers */
srand ( time (NULL) ); /* make a random number based on unix time */
printf ("Your lotto numbers are: n");
for ( num = 0; num < NUMBERS; num++ ) { /* repeat for how many numbers you want */
if ( ( random = rand () % MAX ) == 0 )
printf ( "t%d", rand () % MAX );
else
printf ( "t%d", random );
}
printf ("n");
break;
case 2: /* get 5 sets of 6 random numbers */
srand ( time (NULL) ); /* make a random number based on unix time */
printf ("Here's five sets of lotto pics for ya: n");
for ( pick5 = 0; pick5 < 5; pick5++ ) {
printf ("Set %d:n", pick5);
for ( num = 0; num < NUMBERS; num++ ) {
if ( ( random = rand () % MAX ) == 0 )
printf ( "t%d", rand () %MAX );
else
printf ( "t%d", random );
}
printf ("n");
}
break;
default:
printf ("You messed up stupid. Try againn");
}
return 0;
}
This will output something like the following:
[jgerold@jgerold-mbp13.smsi.com ~/Dropbox/Public/Development/C/lotto]$ ./lotto
Hey Chris, what would you like to do today?
>> Type number you wish to execute:
(1) Standard Lotto
(2) Pick5 Lotto
1
Your lotto numbers are:
23 33 4 35 14 1
[jgerold@jgerold-mbp13.smsi.com ~/Dropbox/Public/Development/C/lotto]$ ./lotto
Hey Chris, what would you like to do today?
>> Type number you wish to execute:
(1) Standard Lotto
(2) Pick5 Lotto
2
Here's five sets of lotto pics for ya:
Set 0:
14 40 1 1 28 36
Set 1:
15 45 17 11 44 45
Set 2:
6 2 37 43 19 25
Set 3:
37 4 31 16 11 9
Set 4:
38 37 25 17 1 44