0
votes

So I need to create a histogram that shows the results of the sum of the rolls of 4 dice. The program takes the total of the dice each time it rolls. I need to create a histogram of the frequency of each number in the dice roll loop. here is my code:

import java.util.Scanner;
import java.util.Random;
import java.util.Scanner;
import java.lang.Math;
public class DieSimulator
{
public static void main(String[] args)
{
  Die d1 = new Die(6);
  Die d2 = new Die(6);
  Die d3 = new Die(6);
  Die d4 = new Die(6);
  int roll = 100;


  int [] rolls = new int[roll];

     int four = 0;
     int five = 0;
     int six = 0;
     int seven = 0;
     int eight = 0;
     int nine = 0;
     int ten = 0;
     int eleven = 0;   
     int twelve = 0;
     int thirteen = 0;
     int fourteen = 0;
     int fifteen = 0;
     int sixteen = 0;
     int seventeen = 0;
     int eighteen = 0;
     int nineteen = 0;
     int twenty = 0;
     int twentyone = 0;
     int twentytwo = 0;
     int twentythree = 0;
     int twentyfour = 0;

  for (int i=0; i < rolls.length; i++)
{
     int n1 = d1.cast();
     //System.out.print(n1 + "");
     int n2 = d2.cast();
     //System.out.print(n2 + "");
     int n3 = d3.cast();
     //System.out.print(n3 + "");
     int n4 = d4.cast();

     int sum = n1 + n2 + n3 + n4;

     if (sum == 4)
         four++;
     if (sum == 5)
         five++;
     if (sum == 6)
         six++;
     if (sum == 7)
         seven++;
     if (sum == 8)
         eight++;
     if (sum == 9)
         nine++;
     if (sum == 10)
         ten++;
     if (sum == 11)
         eleven++;
     if (sum == 12)
         twelve++;
     if (sum == 13)
        thirteen++;
     if (sum == 14)
        fourteen++;
     if (sum == 15)
        fifteen++;
     if (sum == 16)
        sixteen++;
     if (sum == 17)
        seventeen++;
     if (sum == 18)
        eighteen++;
     if (sum == 19)
        nineteen++;
     if (sum == 20)
        twenty++;
     if (sum == 21)
        twentyone++;
     if (sum == 22)
        twentytwo++;
     if (sum == 23)
        twentythree++;
     if (sum == 24)
        twentyfour++;

     System.out.println("Total Dice roll: " + sum);
     System.out.println(twenty);
     }

     int[] histogram = new int[20];


     System.out.print(histogram);


   }


}

What code do I use to create the histogram?

2
What are the 4 to 24 variables ? Couldn't you use an array there ? - nha
those are all the possible sums of the dice. Is there a way to include those variables in the array. that is really what I am trying to do. - Ibrewster

2 Answers

0
votes

Change to an array like this. One could use SparseArray since indices 0, 1, 2, 3 are never used but it doesn't seem worth it to me. I made the array 25 long (24 + 1) so that results[x] holds the number of times a dice roll of x occurred.

import java.util.Scanner;
import java.util.Random;
import java.util.Scanner;
import java.lang.Math;
public class DieSimulator
{
public static void main(String[] args)
{
  Die d1 = new Die(6);
  Die d2 = new Die(6);
  Die d3 = new Die(6);
  Die d4 = new Die(6);
  int roll = 100;


  int [] rolls = new int[roll];
  int [] results = new int[25];


  for (int i=0; i < rolls.length; i++)
{
     int n1 = d1.cast();
     //System.out.print(n1 + "");
     int n2 = d2.cast();
     //System.out.print(n2 + "");
     int n3 = d3.cast();
     //System.out.print(n3 + "");
     int n4 = d4.cast();

     int sum = n1 + n2 + n3 + n4;
     results[sum]++;

     System.out.println("Total Dice roll: " + sum);
     }

     System.out.print(results);
   }
}
0
votes

Complementing RobP's answer, to get the histogram, add this before the main function's end:

for(int i = 4; i < 24; i++) { 
    System.out.println("Result: " + i + " has got " + results[i] + " hit(s)");
}

I also notice you're not accessing the rolls array. You just use its size to limit your for's iterations.

for (int i = 0; i < 100; i++) 

would do the same... and not allocate the array in memory.