0

I have difficulty in understanding some line of code in this roll die JAVA program using arrays. This program notes the frequency of the numbers 1-6 and then displays in console.

import java.security.SecureRandom;

public class RollDie{

    public static void main(String[] args){
        SecureRandom randomNumbers=new SecureRandom();
        int[] frequency=new int[7]; //array of frequency counter

        //roll die 6000000 times, use die value as frequency index
        for(int i=1; i<=6000000;i++)
            ++frequency[1+randomNumbers.nextInt(6)];
        System.out.printf("%s%10s%n","face","frequency");

        for(int face=1;face<frequency.length;face++)
            System.out.printf("%4d%10d%n", face, frequency[face]);
    }
}

can anyone explain this line:

++frequency[1+randomNumbers.nextInt(6)];
tkausl
  • 13,686
  • 2
  • 33
  • 50
ok_ape
  • 79
  • 6

2 Answers2

0

I have divided it in execution sequence to be easier to comprehend. The order of evaluation follows the following sequence:

1.randomNumbers.nextInt(6) Gets a random number from 0 to 5 inclusive
2. 1 + randomNumbers.nextInt(6) - adds one

  1. frequency[1+randomNumbers.nextInt(6)] - Extracts the value from frequency under the already calculated index.
  2. ++frequency[1+randomNumbers.nextInt(6)] - *adds 1 to that element in frequency *

The difference between the old frequency value and the new frequency value on that position is 1.

Essentially this question relates to the precedence of operators in Java and has been already discussed in details here What are the rules for evaluation order in Java?

Community
  • 1
  • 1
Alexander Petrov
  • 9,204
  • 31
  • 70
  • 1
    So frequency has 7 elements from 0 to 6. This logic statement finds the element from 0 to 6 and adds on to it and then what happens in the 5th step? – ok_ape Jul 10 '16 at 09:01
  • Nothing happens there I have just placed it in order to clarify that the old element in frequency is now increased by one. There is no step 5. Probably I should remove it from the list. It was more a clarification. – Alexander Petrov Jul 10 '16 at 09:02
  • Sorry it is exclusive six the nextRand(6) so it is from 0 to 5 – Alexander Petrov Jul 10 '16 at 09:03
  • Thank You Sir, I got some idea! – ok_ape Jul 10 '16 at 09:07
0

The line is written quite unclearly. I took a long time comprehending it as well.

First, we see the ++ prefix operator. You know what it does right? It increments the variable on the left before any other operators are evaluated. Since there aren't any other operators, it just increments frequency[1+randomNumbers.nextInt(6)] by 1.

So what is this frequency[1+randomNumbers.nextInt(6)] thingy?

First, you would see that it is accessing the array frequency, since there are those [] things. So which index to access? Well, 1+randomNumbers.nextInt(6)!

nextInt(x) returns a uniformly distributed random number between 0 and x-1. nextInt(6) returns a random number between 0 and 5. But a typical dice roll always results in 1 - 6! That's why we need to add 1 to the return value of nextInt. As a result, the index will be between 1 - 6.

Thus, as a whole, this whole line just rolls the dice (1+randomNumbers.nextInt(6)), and increment the corresponding index of the array frequency.

It would probably be easier to read if it were written like this:

int diceRoll = randomNumbers.nextInt(6);
frequency[diceRoll]++;
Sweeper
  • 213,210
  • 22
  • 193
  • 313