1

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.

Sean Patrick Floyd
  • 274,607
  • 58
  • 445
  • 566
clink
  • 193
  • 1
  • 9

2 Answers2

1

Your logic should be:

  • Make some number of dice rolls, and record the number of non equal rolls, and the total number of rolls
  • Then take the average outside of the loop

Something like this:

static final int NUM_ROLLS = 1000;

for (int i=0; i < NUM_ROLLS; ++i) {
    int dice1 = getRandom(1,6);
    int dice2 = getRandom(1,6);

    if (dice1 != dice2) {
        count++;
    }
}

double average = 1.0*count / NUM_ROLLS;
System.out.println(average)

Note carefully that in the division multiplies by 1.0, to force double precision division.

The biggest problem I saw in your code is that you were taking the average inside the loop, which doesn't make sense, because your tally of non equal rolls has not yet finished.

Tim Biegeleisen
  • 387,723
  • 20
  • 200
  • 263
1

Name your variables better.

  • Instead of i, call it rolls
  • Instead of count, call it nonmatching.

Then you'll see that you have this third, unnecessary variable called j which is really not providing anything to the solution (except being a main part of the source of your current problem).

Naming variables in a way that they provide you value is a hard thing. Get started on it early, it will save you more time when rereading your code than any other skill.

For pointers, read up on "intentional naming" which means, "naming the variable how you intend to use it" and not the obvious "naming it the way you intended to name it" (I swear I heard a developer describe it that way to me /shudder/)

Yes, you can do the calculation once, outside of the loop, too. But you'd get the right answer if you started with the right inputs.

Edwin Buck
  • 64,804
  • 7
  • 90
  • 127
  • Also note that the code in the question uses integer division, which likely won't give you (the OP) the answer you expect. – yshavit Apr 18 '18 at 02:19
  • 1
    @yshavit Fair point. I made the use of the extra `j` variable not sound like the only problem. As my point is about choosing good names, I didn't get into integer vs real division. – Edwin Buck Apr 18 '18 at 02:23