What the program does is “rolling a dice” 99 times (which smells like an “off by one” error) and count how often each number was found. The array freq holds at position i how often i was diced.
The code in question generates the next random number and then increments the respective slot in the array.
++freq[1 + Ran.nextInt(6)];
It will become more clear if we break it apart.
- First,
Ran.nextInt(6) is evaluated which yields a random integer from the set {0, 1, 2, 3, 4, 5}. To this, 1 is added to get a number from the set {1, 2, 3, 4, 5, 6}. Let's store this intermediate result away in a separate variable for clearness: int result = Ran.nextInt(6) + 1.
- Next, the respective frequency is looked up in the array and incremented by one.
++freq[result] which has the same effect as freq[result] += 1.
The length of the array freq was made to be 7 so the “natural” index can be used. That is, the element freq[0] is wasted. (I doubt that this is very good style.)
Another word on style: It is generally accepted practice to reserve capital names for types and not use them for variables, so Ran should really be named ran to avoid confusion.
One further addition: The difference between the post-increment operator as in i++ and the pre-increment operator as in ++i is that the result of the former expression is the value of i before the increment while that of the latter is the value after the increment. In your case, it makes no difference because you are not using the result anyway. To see how this is working, try running the following code:
int i = 10;
System.out.printf("i = %d%n", i);
System.out.printf("i++ = %d%n", i++);
System.out.printf("i = %d%n", i);
System.out.printf("++i = %d%n", ++i);
System.out.printf("i = %d%n", i);