I'm supposed to take an array of integers that starts from 0 and goes up to the length-1 of some other int array called cards (that has a user-inputted length) and completely randomize it such that no number is in its original position. I figured out how to generate the first array, but I have absolutely no idea how to completely randomize an array, can anyone help? So far I have:
int size = cards.length;
int[] numberList = new int[size];
for(int i = 0; i<size; i++){
numberList[i] = i;
}
Update:
private int[] shuffleIndex(){
int size = cards.length;
int[] numberList = new int[size];;
for(int i = 0; i<size; i++){
numberList[i] = i;
}
randomizer(numberList);
return numberList;
}
private int[] randomizer(int[] input){
int size = input.length;
Random random = new Random();
for(int i = size -1; i>0; i--){
int j = random.nextInt(i+1);
int temp = input[i];
input[i] = input[j];
input[j] = temp;
}
for(int i = 0; i<size; i++){
if(input[i] == i){
randomizer(input);
}
}
return input;
}
Aucun commentaire:
Enregistrer un commentaire