Dice Probabilities
Required Knowledge Before Starting
- Using the Random class (Chapter 17).
- Parsing user input (Chapter 8).
- Making classes and properties (Chapter 18 and 19) are not required, but will probably be helpful.
- Dictionaries or Lists (Chapter 25).
Problem Description
Modify your solution to the Dice Notation problem to not print out a single result, but run a simulation of lots of rolls (10000 is a good number of iterations).
Keep track of how frequently each result comes up. A Dictionary might be useful for storing these values. (The key can be the number rolled, and the value can be the number of times that the value occurred.) If you do, Dictionary
’s ContainsKey
method might be helpful to allow you to know whether you need to add a new value to the dictionary or just update an existing value.
Print out each encountered outcome and the percent of the time that the outcome occurred. For example, your output might look like this, for an input of “2d4”:
Enter dice: 2d4
2: 6.42%
3: 12.52%
4: 18.85%
5: 24.7%
6: 18.68%
7: 12.35%
8: 6.48%
Notes
With the format specified, the minimum possible outcome of a die roll is #dice + the amount added (or subtracted). The maximum is #sides * #dice + amount added (or subtracted). This can be used to determine the indexing on a for loop for printing results and other things.
Optional Features
- Printing the values out in numeric order is desirable, but not required.
- Values in the allowed range can be displayed (0%) or not.