Random function hints
This page provides some tips on using the C random functions. The next couple paragraphs provide some specific information about how these functions work. You don't need to understand how these functions work to use them. So, the examples and summary which follow the description below will show how to use these in your program. First, a little background…
So that moles don't show up in the same places for the same amounts of time each time the game is played, we must randomly generate positions and times for the moles. C provides us with a function to generate random numbers called rand(). It takes no input parameters and returns a pseudo-random integer between 0 and RAND_MAX. Don't worry about the value of RAND_MAX. It's value is defined in the header file stdlib.h and is plenty big for our purposes. (Actually, the value of RAND_MAX is platform dependent.) So, we make a single call to the rand() function each time we want a random number, as in:
int rand_int;
rand_int = rand();
One word of warning: computers can't really generate random numbers! In fact, if you make repeated calls to the function rand(), you'll notice that the sequence of numbers it generates will repeat. So, C provides us with another function called srand() which is used to seed the random number generator. This helps make the sequence of numbers generated by rand() 'more random'. (Seeding the generator with the same number produces the same sequence of numbers. So, this doesn't give us truly random numbers, but it is good enough for our purposes.) We only want to seed the random number generator once. So, make a single call to the srand() function at the beginning of your main function, just after the variable declarations. Do not make any more calls to it.
srand() takes the number used to seed the random generator as an input parameter and has no return value. In order to provide it with an appropriate seed, we are going to use the current time of day as the input parameter or seed. We can get this with another C function called time().
Example using rand
Here is an example using rand() and srand() from the MSVC help files (the important lines are in bold):
/* RAND.C: This program seeds the random-number generator * with the time, then displays 10 random integers. */ #include <stdlib.h> /*Must include this to use the rand() and srand() functions!!!*/ #include <stdio.h> #include <time.h> /*Must include this to use the time() function to seed the random number generator!!!*/ void main( void ) { int i; /* Seed the random-number generator with current time so that * the numbers will be different every time we run. * Only call this function once at the beginning of * your main program (after the variable declarations). * Make sure to call it just like this! */ srand( (unsigned)time( NULL ) ); /* Display 10 numbers. */ for( i = 0; i < 10;i++ ) printf( " %6d\n", rand() ); /*Make a call to rand to generate a random number*/ }
Example limiting the range of rand
In our whack-a-mole program, we want to generate random spots on the board for the moles to show up. There are only 4 possible columns and 4 possible rows in which a mole can appear. So rather than using the random number between 0 and RAND_MAX, we want to limit the number returned by the rand() function. We want it to be between 0-3 to represent the 4 different columns (or rows) at which the mole might appear. We can do this using the modulus operator, %. Remember that % gives us the remainder of an integer division. So, if we modulus the random number returned by rand() by 4, e.g.
int random_column;
random_column = rand() % 4;
the result will be a number between 0 and 3. This is how we can generate a random number to represent which column (and then which row) the mole should be in. The same ideas apply to having the mole appear for a random amount of time... or other random elements of your program. We'll let you figure it out for the rest of these.
Summary
Here are the important pieces to remember for using the random number functions for this HW5:
#include <stdlib.h>
#include <time.h>
srand( (unsigned)time( NULL ) );
rand()
rand() % 4 /*produce a random number between 0 and 3 */