0

I am no good with programming. I am only taking it because I have to. I am majoring in Business Information Technology with Computer Network Admin. I have yet to figure out why I have to take programming if I am not going to really need it.

Anyhow, I am writing a code that involves rolling dice.

Write a java application to simulate the rolling of two dice 1000 times, assuming a regular six-faced dice. The program should have no input, and should use pseudo random numbers to simulate the rolls (one random number per die).

Store the sum resulting from each roll of two dice in an array, determine the number of times each 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, and 12 was rolled, and determine the number of the roll on which the last 7 and the number of the roll on which last 11 occurred.

SUGGESTION 1: Use 2 arrays, one containing 1000 elements to contain the 1000 results of rolling the 1000 pairs of dice, and the other containing 11 elements for summing the frequencies of the sums of the spots on the upper-faces of the dice.

The output should indicate the frequency of each possible sum resulting from a roll, and should indicate the rolls on which the last 7 and the last 11 occurred. Output should go to command line screen.

HEADS-UP 1: If a Java array is to contain exactly 1000 elements, the subscripts (indexes) of the array elements will range from 0 though 999, inclusive; this means the first element has subscript 0, the second element has subscript 1, etc., and the 1000th element has subscript 999. If a Java array is to contain exactly 11 elements, the subscripts (indexes) of the array elements will range from 0 though 10, inclusive; this means the first element has subscript 0, the second element has subscript 1, etc., and the 11th element has subscript 10. Be very careful about such things when determining for loop indexes of loops set up to handle arrays.

This is what I have so far:

import java.util.Random;

public class lab10b_Tuck {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Random randomNumbers = new Random();

        int die1;
        int die2;
        int[] totals = new int[13];

        for (int index = 0; index < totals.length; index++)
            totals[index]=0;

        for (int roll = 1; roll <= 1000; roll++){
            die1 = 1 + randomNumbers.nextInt(6);
            die2 = 1 + randomNumbers.nextInt(6);
            totals[die1 + die2]++;
        }
        System.out.printf(" %12s%20s\n",
                "Sum of Spots on Faces"," Frequency of Sum");

        for (int k = 2; k < totals.length; k++){

        System.out.printf(" %12d%20d\n", k, totals[k]);


        }
    }
    }

I appreciate any help.

Edited by Dani: Formatting fixed

2
Contributors
1
Reply
3
Views
7 Years
Discussion Span
Last Post by firstPerson
0

Hint :

totals[die1 + die2]++;

should be :

totals[roll] = die1 + die2;

and make sure totals can hold up to 1000 dice roll information.

Edited by firstPerson: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.