CS 1073 Introductory Programming
for Scientific Applications
Arrays and Dice Examples


Simple Dice Statistics: Here is a simple Java program that records the number of times each spot comes up with a simulated die. (The code in red is what will be replaced in the program after this one.)

Here are results of runs with N successive powers of 10:


Using Arrays: Here is the same program using an array. The outputs are exactly the same, except the number of each roll will be different each time the program is run. (The code in red is new to this program.)


Rolling two dice: Here is the previous program adapted to rolling two dice and keeping track of the sum of the spots. The number of spots is an integer from 2 to 12 inclusive, so we need an array of size 13, where we will just waste positions 0 and 1. (Again the code in red is new to this program.)

Here is the output:

Here is the same program, where the rolling of one die has been separated off into a separate function. Other parts have been simplified a bit. The output is the same, except that the output is random, depending on the values returned by the random number generator. (The code in red is new to this program.)

Finally, here is the program with an extra array holding the expected fraction of rolls of each number of spots that should occur. (The code in red is new to this program.)

And here is the output: