arrays - Random Number Generating in Java -


i new programming , have started learn arrays , doing fine until got array stores random numbers inside of it. line of code below 1 baffles me most.

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

before imported random class , made new object named randomnumbers declared variable frequency array 7 integers. book reading tells me line above rolls die 6 million times , uses die value frequency index.

i understand going through in iteration 6 million times says in body each time, not see in body sets variable equal random number. think biggest thing missing means increment frequency[] because understand within brackets there random number between 1 , 6 being added 1. therefore 6 million iterations should pass frequency[1] through frequency[7] if chance happen, yet though passes them not see how sets equal arrays.

can please explain line of code me step step barney style? can't seem wrap head around it.

that routine broken down this

for (int roll = 1; roll <=6000000; roll++) {     int the_random_number = 1 + randomnumbers.nextint(6);     frequency[the_random_number] = frequency[the_random_number] + 1; } 

the code randomnumbers.nextint(6) returns number between 0 , 5. example, if returns 3, 1 added the_random_number becomes 4. after increase frequency occurence of 4 1 , store in frequency array (frequency[4] = frequency[4] + 1;).


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -