# Homework help - Dice Roll

• 04-27-2013, 11:57 PM
Slick Java
Homework help - Dice Roll
Hi, I am having trouble with this program. The point of this program is to roll a dice, store dice values in an array and print out how much time the value has been occurred. The output should be like this:

1 3

4 1

5 1

2 1

But I am getting this:

1 3

4 1

5 1

1 3

2 1

1 3

I am trying to find a way to get my expected result. The code is below.

Code:

```package com.gavin.work; import java.util.Random; public class DiceRoll {     private final Random ran;     int rolls;     int numberOfDice = 1;     int numberOfThrows = 6;     int numberOfSides = 6;     int[] counts = new int[numberOfThrows];     int[] occurences = new int[numberOfSides * numberOfDice];     public DiceRoll()     {         ran = new Random();     }     public static void main(String args[])     {         DiceRoll d = new DiceRoll();         d.go(Integer.parseInt(args[0]),             Integer.parseInt(args[1]),             Integer.parseInt(args[2]));     }     public int rollDice(int i)     {         return ran.nextInt(numberOfSides) + 1 * i;     }     public void go(int i, int j, int p)     {         numberOfDice = i;         numberOfThrows = j;         numberOfSides = p;         doWork();     }     public void printAmounts()     {         System.out.printf("%d \n %d \n %d", numberOfDice, numberOfThrows,             numberOfSides);     }     public void doWork()     {         for (int h = 0; h < occurences.length; h++)         {             occurences[h] = 0;         }         for (int i = 0; i < counts.length; i++)         {             counts[i] = rollDice(numberOfDice);         }         for (int i : counts)         {             for (int j = 0; j < occurences.length; j++)             {                 if (counts[j] == i)                 {                     occurences[j] = occurences[j] + 1;                 }             }         }         for (int k = 0; k < counts.length; k++)         {             System.out.printf("%d  %d\n\n", counts[k], occurences[k]);         }     } }```
• 04-30-2013, 07:16 AM
kjkrum
Re: Homework help - Dice Roll
The sizes of your arrays and the way you're iterating over them don't seem to make sense. What are counts and occurences supposed to represent? Why are you initializing them with potentially different sizes (lines 18-20) but then iterating over them with a single counter as if they were parallel arrays (lines 81-84)?
• 04-30-2013, 09:30 AM
SurfMan
Re: Homework help - Dice Roll
What are the possible outcomes of a die? 1,2,3,4,5 and 6 (or more, but you could make that a variable). You could use those values (minus one) as indices for the array of outcomes. This would be massively simpler:

This would be your entire loop in a sort of pseudocode:
Code:

```int outcomes[] = new int[6]; //there are 6 sides for ( int i = 0; i < numberOfThrows; i++ ) {   int random = ran.nextInt(6) + 1; // this is the actual number from 1 to 6   outcomes[random - 1]++; //This needs -1 because array indices start at 0 }```
Don't overcomplicate things :)