I'm dying here! So far you guys have been way more help than my instructor who speaks Russian or something. Any advice will be more than helpful.
This is my issue...
Use the random class once for each die, the sum of the two values should then be calculated. Each die can show an int value from 1-6 so the sum with vary from 2-12 with 7 being the most frequent. The dice should roll 36,000 times. I have to use a 1 dimensional array to tally the number of times each sum appears. Results need to be displayed in a tabular format.
I don't know what I'm doing wrong but I'm positive the results are so wrong!
import java.util.Random;
public class dice{
public static int rollnumber;
public static int ROLL_LIMIT = 36000;
public static int[] die1= new int[ROLL_LIMIT];
public static int[] die2= new int[ROLL_LIMIT];
//This function keeps track of how many rolls
//have occured and returns false if it reaches
//ROLL_LIMIT
public static boolean rolltrack(){
++rollnumber;
if(rollnumber == ROLL_LIMIT) {
return false;
}
else {
return true;
}
}
//Generates a random number for each array
//of die numbers. The numbers that are generated
//aren't very random, probably due to the high speed
//at which the function is continuously called.
public static void rollDice(){
Random rand = new Random();
die1[rollnumber] = rand.nextInt(6)+1;
die2[rollnumber] = rand.nextInt(6)+1;
}
//This function calculates the statistic of how much
//each number was rolled for each die, then outputs
//the data as text
public static void tell_roll_statistics()
{
int[] number_rolls1=new int[6];
int[] number_rolls2=new int[6];
double[] number_percent1=new double[6];
double[] number_percent2=new double[6];
for(int j=0;j<6;j++){
for(int k=0;k<ROLL_LIMIT;k++){
if(die1[k] == (j+1)) {
++number_rolls1[j]; //Holds the amount of rolls for each number
}//Holds the amount of rolls for each number
if(die2[k] == (j+1)) {
//in the array number_rolls_percent1 and 2
++number_rolls2[j];
}
}
number_percent1[j] = ((double)number_rolls1[j]/ROLL_LIMIT)*10 ;
number_percent2[j] = ((double)number_rolls2[j]/ROLL_LIMIT)*10 ;
System.out.println("[Die1] % of number "+(j+1)+": "+number_percent1[j]);
System.out.println("[Die2] % of number "+(j+1)+": "+number_percent2[j]);
}
}
public static void main(String args[]){
rollnumber = 0;
do{
rollDice();
}while(rolltrack());
tell_roll_statistics();
}
}