Roll Em

I l @ ve RuBoard

Roll 'Em

We are going to simulate that very popular random activity, dice-rolling. The most popular form of dice-rolling uses two six-sided dice, but there are other possibilities. Many adventure-fantasy games use all of the five geometrically possible dice: 4, 6, 8, 12, and 20 sides. Those clever ancient Greeks proved that there are but five regular solids having all faces the same shape and size , and these solids are the basis for the dice varieties. You could make dice with other numbers of sides, but the faces would not all be the same, so they wouldn't all have equal odds of turning up.

Computer calculations aren't limited by these geometric considerations, so we can devise an electronic die that has any number of sides. Let's start with six sides and then generalize.

We want a random number from 1 to 6.However, rand() produces an integer in the range 0 to RAND_MAX ; RAND_MAX is defined in stdlib.h . It is typically INT_MAX . Therefore, we have some adjustments to make. Here's one approach:

  1. Take the random number modulus 6. It produces an integer in the range 0 through 5.

  2. Add 1. The new number is in the range 1 through 6.

  3. To generalize, just replace the number 6 in step 1 by thew number of sides.

Listing 13.8 shows a function that does these steps for a general number of sides. The listing includes some explicit type casts to emphasize where type conversions take place.

Listing 13.8 The diceroll.c file.
 /* diceroll.c -- dice role simulation*/ #include <stdlib.h>  /* for rand() */ int rollem(int sides) {     int roll;     roll = rand() % sides + 1;     return roll; } 

Listing 13.9 shows a program that uses the rand() , srand () , and rollem() functions.

Listing 13.9 The manydice.c program.
 /* manydice.c -- multiple dice roll */ /* compile with diceroll.c          */ #include <stdio.h> #include <stdlib.h>  /* for srand() */ #include <time.h>    /* for time() */ extern int rollem(int); int main(void) {   int dice, count, roll;   int sides;   srand((unsigned int) time(0));  /* randomize rand() */   printf("Enter the number of sides per die, 0 to stop.\n");   while (scanf("%d", &sides) == 1 && sides > 0)   {       printf("How many dice?\n");       scanf("%d", &dice);       for (roll = 0, count = 0; count < dice; count++)           roll += rollem(sides);           /* running total of dice pips */       printf("You have rolled a %d using %d %d-sided dice.\n",               roll, dice, sides);       printf("How many sides? Enter 0 to stop.\n");   }   printf("GOOD FORTUNE TO YOU!\n");   return 0; } 

Compile Listing 13.9 with the file containing Listing 13.8, and then use the resulting program. The output should look like this:

 Enter the number of sides per die, 0 to stop. 6 How many dice? 2 You have rolled a 10 using 2 6-sided dice. How many sides? Enter 0 to stop. 6 How many dice? 2 You have rolled a 11 using 2 6-sided dice. How many sides? Enter 0 to stop. 0 GOOD FORTUNE TO YOU! 

You can use rollem() in many ways. With sides equal to 2, the program simulates a coin toss with "heads" being 2 and " tails " being 1 (or vice versa, if you really prefer it). You can easily modify the program to show the individual results as well as the total, or you can construct a craps simulator. If you require a large number of rolls, as in some role-playing games, you can easily modify the program to produce output like this:

 Enter the number of sets; enter q to stop. 18 How many sides and how many dice? 6 3 Here are 18 sets of 3 6-sided throws.   12  10   6   9   8  14   8  15   9  14  12  17  11   7  10   13   8  14 How many sets? Enter q to stop. q 

Another use for rand1() or rand() (but not of rollem() ) is creating a number-guessing program so that the computer chooses and you guess. You can try that yourself.

Now let's develop some more functions. The first project is designing a program that reads in a list of integers and sorts them.

I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net