/* -------------------------------------------------------------------------------------------- Random Contest Winner.c Created by Matt Brewer on January 15, 2007 http://www.macfanatic.net You are free to use and modify this code however you see fit. That is the license, in one line. You did read that right. This was quickly written to generate the number I should use to choose the winner for a contest I had on my website. I needed a random number that would serve as the index in a list of comments. This code does that simply, with a lot of comments thrown in for good meaure. ---------------------------------------------------------------------------------------------------------------------------------- */ #include #include int main (int argc, char **argv) { // Check to make sure that we have our arguments if ( argc != 2 ) { printf("usage: random_gen \n"); exit(1); } // Convert the second arg to an integer value (the number of valid comments) int num_valid; int result = sscanf(argv[1], "%d", &num_valid); if ( result == 0 ) { printf("usage: num_valid_comments wasn't an integer\n"); exit(1); } // Generate our random number. Set random's seed to the system time, otherwise everytime // the app is run, random would return the same number srandom( time(0) ); int random_num = random(); // The winner has to be within bounds of the number that were valid int winner = random_num % num_valid; // Print out the results! printf("The winner of this contest was %d\n", winner); }