Random function hints
This page provides some tips on using the random functions for C/C++. 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 below description will show how to use these in your program. First, a little background…
In order to pick a random cavern (for say, the wumpus), we must randomly generate a number corresponding to a cavern (or one number corresponding to the row of the cavern and one number corresponding to the column of the cavern). C/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/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/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.CPP: This program seeds the random-number generator * with the time, then displays 10 random integers. */ #include <iostream> using namespace std; #include <stdlib.h> // Must include this to use the rand() and srand() functions!!! #include <time.h> // Must include this to use the time() function to seed the random number generator!!! int main( void ) { /* 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( int i = 0; i < 10; i++ ) cout << rand() << endl; // Make a call to rand to generate a random number. return 0; }
Example limiting the range of rand
In our "hunt the wumpus" program, we want to generate a random location for the position of the wumpus (among other things). There are only 7 rows and 7 columns in our game board. 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-6 to represent the 7 different rows or 7 different columns or our board. 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 7, e.g.
int random_suit;
random_row = rand() % 7;
the result will be a number between 0 and 6. This is how we can generate a random number to represent the row/column where our wumpus resides. We'll let you figure it out for the other cavern elements.
Summary
Here are the important pieces to remember for using the random number functions for this HW4:
#include <stdlib.h>
#include <time.h>
srand( (unsigned)time( NULL ) );
rand()
rand() % 7 // produce a random number between 0 and 6