dimanche 30 août 2015

Random Number Generating in Java

I am new to programming and have just started to learn about arrays and was doing just fine until I got to an array that stores random numbers inside of it. The line of code below is the one that baffles me the most.

for (int roll = 1; roll <=6000000; roll++)
     ++frequency[1 + randomNumbers.nextInt(6)];

Before this I imported the Random class and made a new object named randomNumbers as well as declared the variable frequency as an array with 7 integers. The book I am reading tells me that this line above rolls a die 6 million times and uses the die value as frequency index.

I understand that this is going through in iteration 6 million times were it does what it says in the body each time, however I do not see in the body where it sets any variable equal to a random number. I think the biggest thing I am missing is what it means to increment the frequency[] because I understand that within the brackets there is a random number between 1 and 6 being added to 1. So therefore the six million iterations should pass by frequency[1] through frequency[7] if it chance to happen, yet even though it passes them by I do not see how it sets anything equal to those arrays.

Can someone please explain this line of code to me step by step barney style? I just can't seem to wrap my head around it.




Aucun commentaire:

Enregistrer un commentaire