I'm writing a java program that simulates rolling two dices and incrementing a counter whenever both dices do not have the same face. After a number of rolls, i want to print out the average of the count.
int i = 0;
int count = 0;
double average = 0;
int j = 0;
while (i<1000) {
int dice1 = getRandom(1,6);
int dice2 = getRandom(1,6);
if (dice1 != dice2) {
count++;
}
while (j!= 1000) {
average = (count/j);
j++;
}
i++;
}
System.out.println(average)
The program doesn't give me the average as I'm quite sure the nested while loop is written wrongly?
the getRandom() function returns a random value between 1 to 6 since a dice can have 6 values.
private static int getRandom(int n1, int n2){
int retVal = 0;
retVal = n1 + (int) Math.floor(Math.random() * (n2 - n1 + 1));
return retVal;
}
I'm a beginner at java, would appreciate some help on this.