Write an application to simulate the rolling of two eight-sided dice (eight-sided dice are used in various role-playing games). The application should use an object from the class Random to roll the first die and then again to roll the second. Each die can have the values of 1 to 8 and the sum of the dice can have values from 2 to 16. Use a one-dimensional array to tally the number of times each possible sum appears. The application should roll the dice 48,000 times. Display the results in a tabular format and determine if your results are reasonable.

Write an application that is an extension of part A by using a two-dimensional array to tally the actual combinations of rolls. A roll of 2 and 7 would be different than a roll of 7 and 2. The application should roll the dice 64,000 times. Display the results in a tabular format and determine if your results are reasonable.

What do you have so far? We do not give answers to homework assignments!

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rolldice;

import java.util.Random;

/**
 *
 * @author Christopher L. Salgado
 */
public class RollDice {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        RollDice rolldice = new RollDice();
        // do the for loop in main and roll the dice and return result
        rolldice.rollDice(48000);
    }
    // simulate rolling of dice 48000 times

    public void rollDice(int rolls) {
        Random randomNumbers = new Random();
        int die1; // number on first die
        int die2; // number on second die
        int totals[] = new int[ 48000 ]; // frequencies of the sums
        int[] frequency = new int[ 9 ];
        // initialize totals to be the amount given by roll
        for (int index = 1; index < totals.length; index++) {
            totals[ index ] = 16;
        }
        // roll the dice
        for (int roll = 2; roll <= 48000; roll++) {
            ++frequency[ 1 + randomNumbers.nextInt(8) ];
            die1 = 1 + randomNumbers.nextInt(8);
            die2 = 1 + randomNumbers.nextInt(8);
            totals[die1 + die2]++;
        } // end for
        // print the table

What problems or errors do you have?
Please use code tags (icon above and to right) when posting code.

Comment on your code:

int totals[] = new int[ 48000 ]; // frequencies of the sums

What is the max index you expect to get for this array?

For debugging: you can easily print the contents of the array by using the Arrays.toString() method

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.