0
import java.util.*;
import java.lang.*;
public class Main {
public static void main(String[] args) {
 Random dice = new Random();
 int a[]=new int [7];
 for(int i = 1 ; i <=100;i++){
 ++a[1+dice.nextInt(6)];
 }
 System.out.println("Sno\t Values");
 int no;
 for(int i=1;i<a.length;i++){
 System.out.println(i+"\t"+a[i]);
 }
}
}
Sno Values
1 19
2 13
3 16
4 16
5 19
6 18

Can any one please explain this line "++a[1+dice.nextInt(6)]"

i know this program provides random number generated from 1-6 on how many times within the given value

asked Jun 6, 2017 at 9:53
2
  • 1
    That line just counts the frequencies for each number. Commented Jun 6, 2017 at 9:55
  • dice.nextInt will start its range from 0, so adding 1 will make it so dice.nextInt(6) will give you a result from 1 to 6 instead of 0 to 5 Commented Jun 6, 2017 at 9:58

4 Answers 4

4

Mostly, that's just hard to read code. It would be at least slightly simpler to read (IMO) as

a[dice.nextInt(6) + 1]++;

... but it's easier still to understand it if you split things up:

int roll = dice.nextInt(6) + 1;
a[roll]++;

Note that there's no difference between ++foo and foo++ when that's the whole of a statement - using the post-increment form (foo++) is generally easier to read, IMO: work out what you're going to increment, then increment it.

Random.nextInt(6) will return a value between 0 and 5 inclusive - so adding 1 to that result gets you a value between 1 and 6 inclusive.

answered Jun 6, 2017 at 9:56
Sign up to request clarification or add additional context in comments.

6 Comments

i understand a bit still i want to know that "a" can store 7 elements but when i execute it provides a[0]=22 (random) value and same goes for other elements . the question is why its is giving 22 for (0) 32 for (1) like that i mean i gave loop through 100 is it calculating how many times 1 is rolled for 100 times?
@jayanth: Yes, it can store elements 0 to 6 inclusive, due to int a[]=new int [7]. Element 0 isn't used in the code.
i know that part can u explain how the first element is getting 22(random) .i am new to coding
@jayanth: The first element is index 0, which will never have a non-zero value. It's not really clear what you're asking, but it sounds like you should step through the code in a debugger.
@jayanth: I have no idea what that comment was meant to indicate, but again, it sounds like you should use the debugger to help you understand the code more, along with the answers here. Stack Overflow isn't designed to be used as a sort of pair programming tool - we're not going to step through the code for you.
|
4

Yes, first you have

int a[]=new int [7];

which is an array that can hold 7 elements (valid indices being 0-6), all of which have an initial value of 0. Then

++a[1+dice.nextInt(6)];

is saying

int randomIndex = 1 + dice.nextInt(6); // <-- a random value 1 to 6 inclusive
a[randomIndex] = a[randomIndex] + 1;

Which is counting how many ones through sixes are rolled.

answered Jun 6, 2017 at 9:57

Comments

2

++a[1+dice.nextInt(6)]

dice.nextInt(6)= return an integer between 0 and 5

then you add 1 to that value, after that you get the element at that index in the array a and you increase that using the ++ operation

answered Jun 6, 2017 at 9:55

2 Comments

dice.nextInt(6); returns a value between 0 to 5.
@SanketMakani you are right, thanks for the comment/feedback!
2

nextInt() returns a pseudorandom, uniformly distributed value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.

So the value of 1+dice.nextInt(6) will fall between 1 to 6 (both inclusive) and increment the value of a[x] like a counter for x

answered Jun 6, 2017 at 10:00

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.