mardi 20 janvier 2015

Why does this program that generates random numbers keep generating numbers in the hundreds?

I have a really simple question on arrays. I've been watching some tutorials and couldn't understand why the code below gives Frequency outputs as random combinations of 1**. It never gives numbers like 5, 67, 541 etc., it always gives something like 150, 175, 183 etc. I hope I made myself clear. Thanks alot!


Code:



Random rand = new Random();
int freq[] = new int[7];

for(int roll=1; roll<=1000; roll++){

++freq[1+rand.nextInt(6)];
}

System.out.println("Face\tFrequency");

for(int face=1; face<freq.length; face++){
System.out.println(face + "\t" + freq[face]);
}


Sample Output:



Face Frequency

1 176

2 171

3 157

4 159

5 164

6 173




Aucun commentaire:

Enregistrer un commentaire