samedi 14 mars 2015

Ranom generator Error

I want to write a program that is randomizing 1000000 times and finally chooses the number from 1 - 45 that was randomly generated most of the times. The code I have written is this:



public class Randomizer {
public static int[][] numbers = new int[45][2];
public static int maxN = 0;
public static int finalChoice;
public static void main(String[] args){

for(int i = 0; i < 45; i++){
numbers[i][0] = i + 1;
numbers[i][1] = 0;
}

for(int i = 0; i < 1000000; i ++){
int rnd = (int)(Math.random() * 45 + 1);
for(int j = 0; j < 45; j++){
if(rnd == numbers [j] [0]){
numbers[j][1] ++ ;
break;
}
}
}
for(int i = 0; i < 45; i++){
if(maxN < numbers[i][1]){
finalChoice = numbers[i][0];
}
System.out.print(numbers[i][1]+" \t");
}

System.out.println("\nFinal Choice: "+finalChoice);

}



}


The problem is that it keeps printing 45. Where could the error be?





Aucun commentaire:

Enregistrer un commentaire