CS 313E: Bulko

Programming Assignment 1:
Rolling Dice
Due Date: September 15, 11:59 pm

When rolling a six-sided die, there are six possible outcomes: 1, 2, 3, 4, 5, and 6. If the die is fair, each of the six outcomes is equally likely. If you roll two six-sided dice and add the results together, the possible outcomes are 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, and 12. Anyone who plays board games such as Monopoly knows that each of these outcomes are not equally likely: 7 is the most common result, and 2s and 12s are very rare.

Assignment:

In this assignment, you will simulate dice rolls using a pseudorandom number generator. Python provides a set of built-in functions that generate numbers that are not truly random, but are close enough to random for our purposes.

To simulate the roll of two dice, you need to do the following:

You can force the random number generator to produce the same sequence of random numbers by giving it a seed value. You do this by adding the statement random.seed() before your first call to randint. In order to make it easier for the TA to grade the programs, please start your main program this way:

   def main():
      random.seed(1314)

This will result in everyone getting the same "random" dice rolls. (This is another reason why these numbers are called "pseudorandom" numbers: they can't really be random if they can be reproduced.)

Your program should ask the user to enter the number of trials to perform; that is, the number of times it should try rolling the two six-sided dice. After it rolls the dice the required number of times, it should draw a histogram displaying the results.

If you ran your program using a value of 10 for the number of trials, your output should look something like the following:


How many times do you want to roll the dice? 10
Results:  [0, 0, 1, 0, 2, 2, 2, 0, 3, 0, 0]

|                          *      
|              *  *  *     *      
|        *     *  *  *     *      
+--+--+--+--+--+--+--+--+--+--+--+-
   2  3  4  5  6  7  8  9 10 11 12

Notes on the output:

The above histogram looks great if the number of trials you're running is somewhere between 1 and 100. But if you run, say, 10000 trials, the height of the graph will exceed the height of the screen. For this reason, the output of your program should be automatically scaled based on the number of trials you are running.

In the example below, we ran 1000 trials, 54 of which resulted in a roll of 3. Instead of displaying 54 asterisks in the "3" column, we scaled the output by a factor of 10. 54/10 = 5.4, which we rounded to 5, the number of asterisks that we ultimately displayed.


How many times do you want to roll the dice? 1000
Results:  [28, 54, 89, 104, 157, 162, 128, 109, 75, 64, 30]

|              *  *               
|              *  *               
|              *  *               
|              *  *  *            
|              *  *  *            
|              *  *  *  *         
|           *  *  *  *  *         
|        *  *  *  *  *  *         
|        *  *  *  *  *  *  *      
|        *  *  *  *  *  *  *      
|        *  *  *  *  *  *  *  *   
|     *  *  *  *  *  *  *  *  *   
|     *  *  *  *  *  *  *  *  *   
|  *  *  *  *  *  *  *  *  *  *  *
|  *  *  *  *  *  *  *  *  *  *  *
|  *  *  *  *  *  *  *  *  *  *  *
+--+--+--+--+--+--+--+--+--+--+--+-
   2  3  4  5  6  7  8  9 10 11 12

In general:

Turning in the Assignment:

The program that you will write should appear in a file called Dice.py. There is no input data file for this assignment. Submit the file via Canvas before the deadline shown at the top of this assignment by attaching it to the Canvas "Assignment" called "HW1".