Simulation for the rolling of dice with the calculation of sums, frequencies and, percentages

StackOverflow https://stackoverflow.com/questions/23101125

  •  04-07-2023
  •  | 
  •  

Question

I am having trouble calculating out the frequency of the sum of the rolling of a pair of dice. I am told I have to use a one dimensional integer array to count the number of times each possible sum appears in 36000 rolls. I am unsure what I am supposed to do since we just started going over arrays. Here is a link to the instructions: http://s65.photobucket.com/user/jls7884/media/DicePic-page-001_zpsd45d977f.jpg.html?filters[user]=139936213&filters[recent]=1&sort=1&o=0.

Here is the code I have so far:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Dice;

/**
 *
 * @author Jacob
 */
import java.util.Random;
public class Dice 
{
public static void count1 ()
{
    int count;

    int frequency[] = new int[7];
    frequency[1] = 2;

    count = 1;
    while (count <= 6)
    {
        frequency[count] = count + 1;
        count++;
    }

    System.out.printf 
    (
    "%-2d %-2d %-2d %-2d %-2d %-2d \n"
    , frequency [1] , frequency [2] , frequency [3] , frequency [4] , frequency [5] , frequency [6]       
    );
}

public static void count2 ()
{
    int count;

    int frequency[] = new int[8];
    frequency[1] = 3;

    count = 1;
    while (count <= 6)
    {
        frequency[count] = count + 2;
        count++;
    }

    System.out.printf 
    (
    "%-2d %-2d %-2d %-2d %-2d %-2d \n"
    , frequency [1] , frequency [2] , frequency [3] , frequency [4] , frequency [5] , frequency [6]       
    );
}

public static void count3 ()
{
    int count;

    int frequency[] = new int[7];
    frequency[1] = 4;

    count = 1;
    while (count <= 6)
    {
        frequency[count] = count + 3;
        count++;
    }

    System.out.printf 
    (
    "%-2d %-2d %-2d %-2d %-2d %-2d \n"
    , frequency [1] , frequency [2] , frequency [3] , frequency [4] , frequency [5] , frequency [6]       
    );
}

public static void count4 ()
{
    int count;

    int frequency[] = new int[7];
    frequency[1] = 5;

    count = 1;
    while (count <= 6)
    {
        frequency[count] = count + 4;
        count++;
    }

    System.out.printf 
    (
    "%-2d %-2d %-2d %-2d %-2d %-2d \n"
    , frequency [1] , frequency [2] , frequency [3] , frequency [4] , frequency [5] , frequency [6]       
    );
}

public static void count5 ()
{
    int count;

    int frequency[] = new int[7];
    frequency[1] = 6;

    count = 1;
    while (count <= 6)
    {
        frequency[count] = count + 5;
        count++;
    }

    System.out.printf 
    (
    "%-2d %-2d %-2d %-2d %-2d %-2d \n"
    , frequency [1] , frequency [2] , frequency [3] , frequency [4] , frequency [5] , frequency [6]       
    );
}

public static void count6 ()
{
    int count;

    int frequency[] = new int[7];
    frequency[1] = 7;

    count = 1;
    while (count <= 6)
    {
        frequency[count] = count + 6;
        count++;
    }

    System.out.printf 
    (
    "%-2d %-2d %-2d %-2d %-2d %-2d \n"
    , frequency [1] , frequency [2] , frequency [3] , frequency [4] , frequency [5] , frequency [6]       
    );
}

public static void main(String[] args)
{
    int num1 = 1;
    int num2 = 2;
    int num3 = 3;
    int num4 = 4;
    int num5 = 5;
    int num6 = 6;

    int num = 0;
    while (num < 7)
    {
        System.out.printf ("%-3d", num++);
    }

    System.out.println ();
    System.out.printf ("%d: ", num1);
    Dice.count1 ();
    System.out.printf ("%d: ", num2);
    Dice.count2 ();
    System.out.printf ("%d: ", num3);
    Dice.count3 ();
    System.out.printf ("%d: ", num4);
    Dice.count4 ();
    System.out.printf ("%d: ", num5);
    Dice.count5 ();
    System.out.printf ("%d: ", num6);
    Dice.count6 ();

    String sumStr = "Sum";
    String frequencyStr = "Frequency";
    String percentageStr = "Percentage";
    System.out.printf ("%-5s %-12s %s", sumStr, frequencyStr, percentageStr);
    System.out.println ();

    Random rollDie = new Random ();

    for (int i = 1; i <= 36000; i++)
    {
        int die1 = 1 + rollDie.nextInt ((6));
        int die2 = 1 + rollDie.nextInt ((6));

        int sum = die1 + die2;
    }

    int count = 2;
    while (count <= 12)
    {    
        int frequency = 1003;
        double minusFrequency = 36000 - frequency;
        double divideTotal = minusFrequency / 36000;
        double percentageTotal = divideTotal * 100;
        double calculatePercentage = 100 - percentageTotal;
        double percentage = calculatePercentage;

        int sumCount = count++;
        System.out.printf ("%3d %11d %12.1f%% \n",sumCount, frequency, percentage);
    }
}

}

Any help would be appreciated.

Thanks, Jacob

Was it helpful?

Solution

How would you do it on paper?

You would separate a sheet of paper into 12 columns (one for 1, one for 2, etc. until 12). You would then roll the dices 3600 times, and each time you would get a 7, for example, you would add a mark in the column 7. Then you would count the marks in each column, and divide the sum by 3600 to get their frequency.

Just do the same in Java: have an int[] counts array of 12 elements. Then do a loop from 0 to 3600. At each iteration, roll the dices, and increment the array element corresponding to the value you got. So, for example, if you got 7th, yo would increment the 7th element of the array.

OTHER TIPS

Perhaps the instructions ask that you create all the separate methods but I, personally, would code it something like the following. This will populate your array, which you can then use to calculate/print whatever is asked using a for loop. For example, if you want to calculate the percentage that the sum 2 was rolled out of the 36,000 rolls you could do: double percentage = frequencies[0]/36000;

Random ranNum = new Random();
int[] frequencies = new int[11];
int face1;
int face2;
int sum;

Arrays.fill(frequencies, 0);

for(int count = 1; count <= 36000; count++)
{
  face1 = 1+randomNumbers.nextInt(6);
  face2 = 1+randomNumbers.nextInt(6);
  sum = face1 + face2;

  switch(sum)
  {
    case 2: frequencies[0] += 1;
    break;
    case 3: frequencies[1] += 1;
    break;
    case 4: frequencies[2] += 1;
    break;
    case 5: frequencies[3] += 1;
    break;
    case 6: frequencies[4] += 1;
    break;
    case 7: frequencies[5] += 1;
    break;
    case 8: frequencies[6] += 1;
    break;
    case 9: frequencies[7] += 1;
    break;
    case 10: frequencies[8] += 1;
    break;
    case 11: frequencies[9] += 1;
    break;
    case 12: frequencies[10] += 1;
    break;
  }
}

I had a friend help me in class. I apologize for my previous comments. I did not look through the code well enough to understand what it was doing. From what I understand now by looking at both your code and JB nizet's logic is that the switch statement takes the value of the sum that corresponds to each element of the array and increments depending on the number of corresponding sum values that are found. I had originally declared and initialized the array inside of the for loop as well as the print statement hence why I was getting 36000 values. I then moved my array declaration and assignment outside of the loop so I would be able to use it throughout the program then I had fixed my while loop to go from [0, 10] and added two to count in order to get the possible sum values [2, 12] then printed out the frequency by passing the value of count through the frequency array. When writing the equation to calculate out the percentage I ran into the issue of getting 0.0% as my percentage value. I was able to fix that by casting my integer array to a double. For a reference, here is the code I ended up with and a sample of the corresponding output:

Code:

import java.util.Arrays;
import java.util.Random;
public class Dice 
{
public static void main(String[] args)
{
    String sumStr = "Sum";
    String frequencyStr = "Frequency";
    String percentageStr = "Percentage";
    System.out.printf ("%-5s %-12s %s", sumStr, frequencyStr, percentageStr);
    System.out.println ();

    Random rollDie = new Random ();

    int frequencies[] = new int [11];

    Arrays.fill(frequencies, 0);

    for (int i = 1; i <= 36000; i++)
    {
        int die1 = rollDie.nextInt (6) + 1;
        int die2 = rollDie.nextInt (6) + 1;

        int sum = die1 + die2;

        switch (sum)
        {
            case 2: frequencies[0]++;
            break;

            case 3: frequencies[1]++;
            break;

            case 4: frequencies[2]++;
            break;

            case 5: frequencies[3]++;
            break;

            case 6: frequencies[4]++;
            break;

            case 7: frequencies[5]++;
            break;

            case 8: frequencies[6]++;
            break;

            case 9: frequencies[7]++;
            break;

            case 10: frequencies[8]++;
            break;

            case 11: frequencies[9]++;
            break;

            case 12: frequencies[10]++;
            break;

            default:
            break;
        } 
    }

    int count = 0;
    while (count <= 10)
    {    
        double percentage = ((double) frequencies[count] / 36000) * 100;

        System.out.printf ("%3d %11d %12.1f%% \n",count + 2, frequencies[count], percentage);
        int sumCount = count++;
        }
    }
}

Output:

Sum   Frequency    Percentage
  2        1013          2.8% 
  3        1953          5.4% 
  4        2997          8.3% 
  5        4012         11.1% 
  6        5015         13.9% 
  7        6093         16.9% 
  8        4966         13.8% 
  9        3999         11.1% 
 10        3024          8.4% 
 11        1944          5.4% 
 12         984          2.7% 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top