Generating Random Numbers and the Rules of Craps

Generating Pseudo-Random Numbers

1. long rand(void); // generates pseudo-random numbers
    a. Include file: <cstdlib>  or <stdlib.h> (standard library)
    b. Range: 0 <= rand() <= RAND_MAX
    c. Defined in <cstdlib>, RAND_MAX = 2147483647 (231 - 1) on this and most
        systems in which 231 - 1 is maximum value of the long type.
    d. Example: x = rand() % 100 + 1;

2. void srand(long);
    a. Include file: <cstdlib> or <stdlib.h>
    b. Seeds rand() thereby determining the starting-point of sequence of numbers to be generated.
    c. Example: srand(5);
        The 'same' sequence of random numbers is produced every time srand(5) precedes the
         programmed calls to rand().

3. long time(long);
    a. Include file: <ctime> or <time.h>
    b. time(0) returns the current calendar time in seconds.
    c. When used in conjunction with srand(), time() provides a mechanism for varying the seed for srand().
    d. Example: srand( time(0) );
        This need only be done once in a program, before any calls to rand() are made.

    EXAMPLE:
    srand( time(0) );
    die1 = rand() % 6 + 1;
    die2 = rand() % 6 + 1;


The Rules of Craps

A player rolls two dice. Each die has six faces. These faces contain 1, 2, 3, 4, 5, and 6 spots. After the
dice have come to rest, the sum of the spots on the two upward faces is calculated.

    1. If the sum is 7 or 11 on the first throw, the player wins.
    2. If the sum is 2, 3, or 12 on the first throw (called "craps"), the player loses (i.e., the "house" wins).
    3. If the sum is 4, 5, 6, 8, 9, or 10 on the first throw, then that sum becomes the player's "point."
        3a. To win, you must continue to roll the dice until you "make your point."
        3b. The player loses by rolling a 7 before making his or her point.

MCX1 home   MCX2 home