This lab should help you with the concept of random value generation. It also provides more practice with calling library functions.
Write a program that helps the user determine the statistics on a common dice roll. To refresh your memory, you normally pick a size of die and a number of dice to roll. Normally gamers term this NdS, where N is the number of dice and S is the size of each of the die (so 4 12-sided dice would be 4d12).
The minimum value achievable on such a roll is N (since each die has a minimum value of 1 and there are N of them). The maximum value you can get on such a roll is N*S (since the maximum value on each die is S and there are N of them). The average of such a roll is the quantity:
minimum + maximum ------------------- 2
Believe it or not!
Your program needs to calculate these three statistics for the user and report them in a nice way. It would also be nice to give them a sample roll value (i.e. what might possibly come up during an actual roll).
As an example, you might have the program interaction look something like (the parts typed like this are what the user typed):
$ dicestat.out Welcome to the Dice Statistics Program!!! How many dice in your dice roll? 3 How many sides on the dice in your dice roll? 12 Thank you!! Calculating... Done. When rolling 3 size-12 dice, your statistics will be: Minimum: 3 Average: 19.5 Maximum: 36 A typical dice roll might result in 23. Thank you for using the DSP!! Endeavor to have an extemporaneous day! $
Note how the average is a decimal value even though there is no possible way to roll a 19.5 on a standard die. (That's statistics for ya'!)
This assignment is (Level 2).
Traditional gamers using your program find the interaction a bit clumsy. They are used to the 'traditional' notation that you print in the report at the end (NdS) instead of entering N at one prompt and S at another. It is in their guide/rulebooks as NdS, it is on their character sheets as NdS, and it is even in their heads as NdS, but they can't use it here!
Add (Level 1) to make the user to enter the dice roll in normal style. (i.e. number of dice, the letter d, and then the number of sides on each die.)
The program interaction might now look something like (the parts displayed here like so are typed by the user):
$ dicestat.out Welcome to the Dice Statistics Program!!! Enter your dice roll: 3d12 Thank you!! Calculating... Done. When rolling 3d12 (3 12-sided dice), your statistics will be: Minimum: 3 Average: 19.5 Maximum: 36 A typical roll of 3d12 might end up with a value of 15. Thank you for using the DSP!! Endeavor to have an extemporaneous day! $
The astute mathematician or the terribly bored gamer will notice that your sample roll isn't coming out quite right. It's okay when only a single die is rolled... But if more than one die is in the roll, your program prints the middle values less often than it should (or, alternatively, your program prints the tail values more often than it should). In other words, your program is printing the values selected from a uniformly distributed range when the real values (when you have actual dice involved) are not uniformly distributed but more 'bell' shaped.
Add (Level 2) to make your sample roll more statistically accurate. Using the simple formula we developed in class, it will be a value in the proper range, but uniformly distributed (each value will be equally likely). But that simply isn't the case! Look at something as simple as 2d4:
die 1 | die 2 | total --------+---------+--------- 1 | 1 | 2 1 | 2 | 3 1 | 3 | 4 1 | 4 | 5 --------+---------+--------- 2 | 1 | 3 2 | 2 | 4 2 | 3 | 5 2 | 4 | 6 --------+---------+--------- 3 | 1 | 4 3 | 2 | 5 3 | 3 | 6 3 | 4 | 7 --------+---------+--------- 4 | 1 | 5 4 | 2 | 6 4 | 3 | 7 4 | 4 | 8 dice total | 2 | 3 | 4 | 5 | 6 | 7 | 8 ----------------+-----+-----+-----+-----+-----+-----+----- ways to get it | 1 | 2 | 3 | 4 | 3 | 2 | 1 2|* 3|** * 4|*** * * * 5|**** * * * * * 6|*** * * * * * * * 7|** --------------------- 8|* 2 3 4 5 6 7 8
The first table shows all possible rolls and totals for the two 4-sided dice. The second table shows the possible totals and the number of ways each can be arrived at. The bottom two 'histograms' (and I use that term lightly) represent the same data as in the second table. Notice that a 5 total is 4 times as likely as either a 2 or an 8 total.
Although we could use some rather twisted statistics to model this behavior, it would be far easier to roll the dice individually and add up their respective values. This means repeating the rolling of a single die a number of times equal to the number of dice to be used. The roll of a single die has, of course, a minimum of 1 and a maximum of the number of sides on the die.
(Hint: Repetition might be discussed in Chapter 3...)
If you did all above options, this lab could be worth as much as (Level 5).