0

Can someone explain what does ++freq in my given program below?

package array;

import java.util.Random;

public class CounterArray {
     public static void main(String[] args){
         Random Ran = new Random();
         int freq[] = new int [7];

         for(int roll=1;roll<100;roll++){
             ++freq[1+Ran.nextInt(6)];
         }
          System.out.println("face\tFrequency");
         for(int face=1;face<freq.length;face++){
             System.out.println(face+"\t"+freq[face]);
         }
     }
}

5 Answers 5

5

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Great, just what if freq[1 + Ran.nextInt(6)]++ be used instead of ++freq[1 + Ran.nextInt(6)] does it effects on the mechanism of getting random number between 1..6 or no they are the same?
great, I just wanted to be sure that way has not any effect on getting random mechanism by ran.nextint() which is inside the brackets. +1
2

It increments by one a random element from the array freq.

 ++freq[1+Ran.nextInt(6)];

The Ran.nextInt(6) part gets a random integer between 0 and 5. The 1+ part adds one to the random number. So if the random number is say, 3, you effectively have ++freq[4] (after addition). Since freq[4] is just an integer reference, you can increment it. By using the prefix ++, you are saying "add one to whatever number comes after me, and set/return it".

So what you are doing is finding a random element in the array and incrementing it by 1.

Comments

1

It simply increments by one the value at the position of the array obtained by 1+Ran.nextInt(6). In a more "descriptive" way, you can interpret it as simulating that the dice face you obtained was 1+Ran.nextInt(6), and so, the frequency of that face augments by one.

Comments

1

It is the increment operator and the behavior will be like the following:

int temp = 1+Ran.nextInt(6);
freq[temp] = freq[temp] + 1;

The part Ran.nextInt(6) will generate a number between 0 and 5. So 1+Ran.nextInt(6) will return a number between 1 and 6. The code will use this number as the index of the array to find which element should be increased by 1.

3 Comments

This is not correct because Ran.nextInt(6) will very likely return different answers each time called.
still i can't got it :(
It's still wrong. It should be freq[temp] = freq[temp] + 1; following your definition of temp.
1

You need to know something about some elements used:

  • The ++ operator increments the preceding or following variable.
  • The method Random.nextInt(int) returns an integer.
  • You have multiple operations on this line: ++freq[1+Ran.nextInt(6)].

What happens is this:

  1. Random.nextInt(6) returns any integer between 0 and 5, say 4.
  2. With this snippet: 1 + Ran.nextInt(6), you increment 4 with 1, which results in 5.
  3. So you're calling index 5 of the freq integer array.
  4. Since you have only initialized the size of the array, and not set any values, all values default to 0a.
  5. freq[5] returns 0, because the value of position 5 of the freq array is 0a.

If we rewrite the snippet ++freq[1+Ran.nextInt(6)], it will look like this:

int diceResult = Ran.nextInt(6); // Returns, say, 4
int index = 1 + diceResult; // We increment dice result by 1
freq[5] = freq[5] + 1;

a Of course, when the loop is repeated, these values might be greater than 0.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.