I am writing this code to run 10000000 times and randomly generate a number and pick the most oftenly generated number, but the results do not seem random. The I have written is
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){
int arithmoi[] = new int[5];
for(int i =0; i < 5; i++){
arithmoi[i] = randomizer(1000000, 45);
System.out.println("A "+i+": "+arithmoi[i]);
}
int extra_num = randomizer(1000000, 20);
System.out.println("\nT: "+extra_num);
}
public static int randomizer(int times, int amount){
int[][] numbers = new int[amount][2];
for(int i = 0; i < amount; i++){
numbers[i][0] = i + 1;
numbers[i][1] = 0;
}
for(int i = 0; i < times; i ++){
int rnd = (int)(Math.random() * amount + 1);
for(int j = 0; j < amount; j++){
if(rnd == numbers [j] [0]){
numbers[j][1] ++ ;
break;
}
}
}
for(int i = 0; i < amount; i++){
if(maxN < numbers[i][1]){
finalChoice = numbers[i][0];
maxN = numbers[i][1];
}
}
return finalChoice;
}
}
The result that it gives me are A 0: 36 A 1: 36 A 2: 36 A 3: 36 A 4: 29
T: 14, A 0: 26 A 1: 44 A 2: 44 A 3: 44 A 4: 44
T: 4 and similar. What could the problem be?
Aucun commentaire:
Enregistrer un commentaire