Click here to Skip to main content
15,887,450 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi, I have a friend which have this code.

Java
import java.util.*;
public class JavaRandomMath {

	
	public static void main (String [] args)
	{
	
		
		Random rand = new Random();
		
		int freq[] = new int [5];
		
		for (int roll = 0; roll <= 5; roll++)
		{
			
			++freq[1 + rand.nextInt(4)];
			
		}
		
	
		System.out.println("Face\tFrequency");
		
		
	for (int face = 0; face < freq.length; face++)
		{
			
			System.out.println(face + "\t" + freq[face]);
			
		}	
		
		
	}

}


I tried to understand this on my own and I can pretty understand the rest aside from this one line
Java
++freq[1 + rand.nextInt(4)];
which is located in the first for loop.

What I have tried:

I tried to use a breakpoint there to see whats happening behind.What I can understand from the debugger is that everytime the first for loop LOOPS, the indices of the freq array is assigned a new value by the random.nextInt.

What I can't understand there is the whole line. The ++freq is really confusing me. It looks like it's incrementing? and after the ++freq is a bracket [] that has this inside
1 + rand.nextInt(4)
I already read the docs of the random class in java. The 1 there maybe used to add on the generated number, but why does it need to be inside this [] and what does ++freq mean before []?
Posted
Updated 24-Apr-20 4:44am

You're right, the ++ is an increment operation.

freq[4] is an array of five integers, indexed by 0 through 4.

rand.nextInt(4) says "Give me the next random integer from 0 up to 4."

When that number is returned, its index in the freq array is incremented. This counts the number of times that each number appeared.
 
Share this answer
 
v4
Comments
Richard Deeming 24-Apr-20 12:05pm    
The next random integer from 0 to 3, surely? The upper bound is exclusive, so it will never return 4.

Random (Java Platform SE 7 )[^]
Greg Utas 24-Apr-20 12:36pm    
Yes, I came back to look at it again after I recalled the 1+ and realized it was fishy. But by that time, OriginalGriff had sorted it out, so I didn't bother to change my solution.
lelouch_vi 2 25-Apr-20 10:54am    
Hi Greg Utas,

Thanks for the explanation.
When that number is returned, its index in the freq array is incremented.

I'm still confused with the ++freq although OriginalGriff break down the code already.
It really looks incrementing but when I added a breakpoint on the for loop the storing of the random number sometimes starts at random indices also.
Greg Utas 25-Apr-20 11:46am    
I don't actually use Java, but it looked enough like C++ that I thought I could answer. If the index is random, then I assume it would sometimes be out of bounds, in which case I believe Java would raise an exception. Is this what happens? Also, if you've changed that line of code, please include the new version.
lelouch_vi 2 26-Apr-20 10:29am    
Hi Greg Utas,

Thanks for all the help.
Just to add to Greg's reply, if you don't understand a line of code, break it down:
++freq[1 + rand.nextInt(4)];
Becomes
int index = 1 + rand.nextInt(4);
++freq[index];
Becomes
int randomValue = rand.nextInt(4);int index = 1 + randomValue;
freq[index] += 1;
Becomes
int randomValue = rand.nextInt(4);
int index = 1 + randomValue;
freq[index] = freq[index] + 1;

Now it's obvious:
Generate a random number that fits in the array
Add one to it.
Add one to that element of the array.

However, that bad code: although rand.nextInt(n) returns a value between 0 and (n - 1) inclusive (See the documentation: Java.util.Random.nextInt() in Java - GeeksforGeeks[^]) adding one to it means it only uses elements 1, 2, 3, and 4 of the array - the value in index 0 will always be zero.
A better solution would be:
Java
++freq[rand.nextInt(freq.length)];
As that will generate values that can be any index in the array, regardless of the size of teh array.
 
Share this answer
 
Comments
lelouch_vi 2 25-Apr-20 10:50am    
Hi OriginalGriff,

Thanks for breaking down the code. I'm studying it.
Thanks for this:
int randomValue = rand.nextInt(4);int index = 1 + randomValue;freq[index] = freq[index] + 1;


I just want to clarify things. This line
freq[index] = freq[index] + 1;

means that the index that will be the storage of the generated number will also be random? Am I correct? I add a breakpoint there and wacth what's happening behind. The storage of the random number sometimes starts at index 3, 2, 4?
OriginalGriff 25-Apr-20 10:56am    
You're not "storing" the random number - you are using it to identify which "column" of the array is counting them.

Try it on paper and see it that make sit any clearer - it's kinda hard to explain without being able to see at what point your eyes glaze over. :laugh:
lelouch_vi 2 25-Apr-20 20:55pm    
Hi OriginalGriff,

I already ready this reply of yours last night and tried to understand this.

So like you said
You're not "storing" the random number - you are using it to identify which "column" of the array is counting them.

In the line:
++freq[1 + rand.nextInt(4)];


The [1 + rand.nextInt(4)] acts as an index of the array freq because it's inside of the [ ]. Since I'm using the random.nextInt inside the [ ] it generates random index for the freq? That's the reason behind why you said you are using it to identify which "column" of the array is counting them.

Since there is a 1 inside [ ] the 4th index of array freq can now be accessed?

The ++freq increments the element of the index (which is randomly generated). So the
[1 + rand.nextInt(4)] has nothing to do with value of the array it just generate the index for the array(randomly).

The ++freq is the one who is responsible for the values because it increments the value of where the index at which is generated by the rand.nextInt(4).

So if I want to increase the the numbers inside of each element I should increase the condition of the for loop? like raise it to
roll < 1000?? Is this the right way to do it?

I hope my eyes are glazing on the right spot now haha.
OriginalGriff 26-Apr-20 3:47am    
Yes. That's right, and the right way to increase the frequency.

But do read what I said again, especially the last bit, starting with "However"
lelouch_vi 2 26-Apr-20 10:28am    
However, that bad code: although rand.nextInt(n) returns a value between 0 and (n - 1) inclusive (See the documentation: Java.util.Random.nextInt() in Java - GeeksforGeeks[^]) adding one to it means it only uses elements 1, 2, 3, and 4 of the array - the value in index 0 will always be zero.

Yes Thank you. Before I came here I already read the docs about the random class.



This line:
++freq[rand.nextInt(freq.length)];

is really the best solution for this.

Thank you so much OriginalGriff

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900