Homework 1: Due 9/6


Using some of the ideas in RandomTest.java write a simple simulation for the process of rolling two n-sided dice and recording the sum of the outcomes. Start by reading a positive integer n; then simulate the rolling of the two dice, 100,000 times. As in RandomTest.java, you should use the Random class, defined in java.util.Random. Note that the sum of the two dice rolls can be any number between 2 and 2* n. Keep track of the number of times each number between 2 and 2 * n appears as the sum of the dice rolls and output this information. Name the file containing this program TwoRolls.java. Make sure that your program is well commented and that variable names are suggestive of their purpose. Along with the file TwoRolls.java, you should also submit a text "readme" file called TwoRolls.readme that lists any errors, peculiar behavior, limitations, etc. of your program. For example, it may be that your program crashes if the user types a negative number for n. If so, the TwoRolls.readme file would be the place to mention this. Alternately, if your program takes a while to complete execution, you should mention this in the TwoRolls.readme file. Based on the output of your simulation, answer the following questions.

  1. Is every sum between 2 and 2*n equally likely? Are there some sums much more likely than others? To support your answers present plots, for n = 6 and n = 10, showing the number of times each sum in the range 2 through 2*n shows up.

  2. Explain precisely why it is that all sums do not occur the same number of times (approximately). Devise a formula for the probability that an integer x in the range 2 through 2*n will show up as a sum. Is the output from your simulation as predicted by your formula?
Provide the answers to these questions (with associated plots) in a pdf file called TwoRolls.pdf. In summary, you should submit three files TwoRolls.java, TwoRolls.readme, and TwoRolls.pdf. Submit a zipped directory containing these three files in the ICON dropbox for homework1.