# Dice Simulation Program

• 03-14-2010, 06:36 AM
touandcim
Dice Simulation Program
So I'm not gonna lie, I need help with homework. I basically have to write a program that simulates the rolling of two die 100000 times. I achieved this using a for loop. Each time a sum comes up (2, 3, 4, 5....12) I increment the corresponding array element (2 corresponds to dieTotalCount[0] and 3 corresponds to dieTotalCount[1] etc.). I then calculate the percentage and of each total (out of 100000) and display the sum values, total times they are rolled and the percentage of times they are rolled out of 100000. I have the whole program written. My problem comes when I am trying to format the percentage(float type). It doesn't show the percentages at all, just the %2.1f for every damn line. If anyone could help, that'd be awesome! Oh yeah. this is the code:

package vangasg9;

/**
*
* @author Tou Vang
*/
import java.util.Random;
public class VangAsg9Dice {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Random randomNumbers = new Random();
int dieOne;
int dieTwo;
int dieSum = 0;
int[] possibleSums = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
int[] dieTotalCount = new int[11];
double[] percentDieTotals = new double[11];
double numberOfRolls = 100000;

//rolls two die 100000 times and totals them each time
//stores them into dieTotalCount array
for (int counter = 1; counter <= 100000; counter ++){
dieOne = 1 + randomNumbers.nextInt(6);
dieTwo = 1 + randomNumbers.nextInt(6);

dieSum = dieOne + dieTwo;

switch (dieSum){
case 2:
++dieTotalCount[0];
break;
case 3:
++dieTotalCount[1];
break;
case 4:
++dieTotalCount[2];
break;
case 5:
++dieTotalCount[3];
break;
case 6:
++dieTotalCount[4];
break;
case 7:
++dieTotalCount[5];
break;
case 8:
++dieTotalCount[6];
break;
case 9:
++dieTotalCount[7];
break;
case 10:
++dieTotalCount[8];
break;
case 11:
++dieTotalCount[9];
break;
case 12:
++dieTotalCount[10];
}//end switch
dieOne = 0;
dieTwo = 0;
dieSum = 0;
}//end for

//calculates the percentage of roll totals for the 11 numbers
for(int counter = 0; counter <= 10; counter++){
percentDieTotals[counter] = (double)dieTotalCount[counter]/numberOfRolls;
}//end for

//output
System.out.println("This is a summary of the sums of two die rolled 100,000 times.\n\n");

System.out.printf("%-10s%-25s%20s", "Sum:", "Number of times rolled:", "Percentage of total:\n");//displays headers

//displays numbers
for(int counter = 0; counter <= 10; counter++){
System.out.printf("%-10s%-25d%25%2.1f\n", possibleSums[counter], dieTotalCount[counter], percentDieTotals[counter]);
}[/SIZE][/SIZE]

}//end main

}
• 03-14-2010, 07:24 PM
collin389
Something like this should work:
Code:

`System.out.printf("%-10s%-25d%2.1f%%\n", possibleSums[counter], dieTotalCount[counter], percentDieTotals[counter]*100);`
Also, please remember to use code tags when posting code.
• 03-15-2010, 07:52 AM
touandcim
thanks a bunch Collin. I had figured out what I was doing wrong already. It's funny how little things like formatting make such a big difference. Sorry bout not using the code tags. I will remember that in the future!

thanks again man.
• 03-15-2010, 01:09 PM
Fubarable
If you use an array if int[13] and ignore the [0] and [1] position, you can simply things greatly including getting rid of that switch statement. Collin's code seems to have disappeared. ???