Comp 1672, Winter 2003
Lab 1
Tuesday January 7, 2003
Thursday, January 9, 2003

Welcome back! The goals of the first lab this quarter are:

To generate random numbers in C++, you can use the standard library function rand, which has this prototype:
int rand();
Note that rand takes no parameters and returns an int. The integer that it returns will be a random number between 0 and the constant RAND_MAX. In order to use the function rand, you need to #include <cstdlib>

Exercise 1

Below is a snippet of code that shows you how to use the function rand.
    cout << "This program prints out 10 random numbers between";
    cout << " 0 and " << RAND_MAX << endl;
    for (i=0; i < 10; i++) {
        cout << rand() << endl;
    }
Write a program using this code, compile it and run it. Please DO NOT retype the above code - instead copy and paste it into your program. When you're done, print out the source code and the output of your program. You'll have to remember Run the program again, and then again. Notice anything about the output? This should disturb you. We expected the rand function to generat random numbers, not the same ones each time. In fact, a computer can only generate what are called pseudorandom numbers, or lists of numbers that appear to be random. In order to inject more apparent randomness into our list, we must initialize the random-number generator, by giving it a number to begin with. This number is called a seed, and we must give the random number generator a different seed each time in order for it to produce a new sequence. To initialize the random number generator, we use the function srand. Here is its prototype:
void srand(unsigned int);
The parameter is the seed, and the function returns no value. Like rand, the function prototype is also found in <cstdlib>. This initialization should be done exactly one time, before the first time that the random number generator is called. But before we call srand, we need to get our hands on a good value for the seed. This number must itself be somewhat random. A great source of seeds which vary is the computer's clock, which is continually and rapidly changing. To do this, use the function time, the prototype for which is found in <ctime>. Putting all of this together, to get a more random sequence of numbers, we just need to add the following line to the beginning of our program:
    srand(time(0));

Exercise 2

Insert the above line in your program, recompile, and execute. Verify that your program now produces a new sequence each time it is run. Print out one of your new outputs and turn it in.

Usually when we are using a random number generator in a program, we don't really want a random number between 0 and RAND_MAX. For example, if we want a one-digit number, then we want a random number between 0 and 9. To do this, we can conveniently use the mod operator. This is called scaling. For example, the following line of code will output a random number between 0 and 9:

    cout << rand()%10;
This technique allows you to generate random integers between 0 and and integer m, m being the scaling factor.

What would you do, however, if you wanted a random number between 1 and 10? There's a trick - just generate a random number between 0 and 9, then add 1 to it. This is known as shifting

Exercise 3

Turn in the following.
  1. Write a line of code that generates a random number between 1 and 10 and stores it.
  2. Write a line of code that generates a random number between 16 and 25 and outputs it.
  3. Write a line of code that generates a random number between 73 and 92 and outputs it.
  4. Write a line of code that generates a random number between a and b and outputs it.

Exercise 4

Let's put all of this together to write a short program. The program simulates rolling of a pair of dice. The program generates two random numbers to represent the number of spots on a pair of dice, and tells them to the user. It repeatedly asks the user if he/she wants to continue, and if so, it outputs another roll of the dice. Print out your source code and turn it in.