Arrayelements as counters

Home / Uncategorized / Arrayelements as counters

I’m learning java and have some misunderstanding about array elements:import java.util.Random;
class One {
public static void main (String[]args) {
Random R1 = new Random();
int mat [] = new int [6];
for ( int chance =0; chance<99; chance++ ) { ++mat[1+R1.nextInt(5)]; } System.out.println("MtNumber"); for(int M =1;M Answer:
I think there is no mistake. It is very likely that numbers will be about 20 because:
You repeat the following sequence 99 times: 1. Get one random(one of six) element from array. 2. Increase element value (plus one).

Let’s assume (of course it doesn’t) that random method works in the way it gets for 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 we get: 20 in first array element 20 in second array element and so on…

So, to get bigger number you need to decrease array length or change condition in for loop to bigger "chance" number (in your case: for ( int chance =0; chance<200; chance++ ) ). Read more

Leave a Reply

Your email address will not be published. Required fields are marked *