java - Arrayelements as counters -


i'm learning java , have misunderstanding array elements:

import java.util.random; class 1 {   public static void main (string[]args) {     random r1 = new random();     int mat [] = new int [6];     ( int chance =0; chance<99; chance++ ) {                     ++mat[1+r1.nextint(5)];              }     system.out.println("m\tnumber");     for(int m =1;m <mat.length;m++) {              system.out.println(m  + "\t" + mat[m]);     }   } } 

and after running code got like:

  m number     1   30     2   15     3   17     4   23     5   14 

i did lot of attempts , number <=20, bit confusing me cause assighn chance <99 hence asume should bigger number in general. point on mistake?

i think there no mistake. numbers 20 because:

you repeat following sequence 99 times: 1. 1 random(one of six) element array. 2. increase element value (plus one).

let's assume (of course doesn't) random method works in way gets 99 execution : 20 times value 0 20 times value 1 20 times value 2 20 times value 3 20 times value 4 19 times value 5

so get: 20 in first array element 20 in second array element , on...

so, bigger number need decrease array length or change condition in loop bigger "chance" number (in case: ( int chance =0; chance<200; chance++ ) ).


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -