I'm a beginner in Java and I have a question regarding Arrays. I've been struggling with this task where it says: Write a program that produces ten random permutations of the numbers 1 to 10. To
generate a random permutation, you need to fill an array with the numbers 1 to 10
so that no two entries of the array have the same contents. Make a second array and fill it with the numbers 1 to 10. Repeat 10 times, Pick a random element from the second array. Remove it and append it to the permutation array. I am not allowed to use Arraylist to solve this task.
This is what I have done so far and my issue right now is that it prints out some zeroes while the rest of the numbers are all different like it should be: [2, 4, 10, 3, 9, 0, 0, 7, 5, 0].
What am I doing wrong, Am I even on the right way? Any help would be appreciated!
int[] myArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] myArray2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] permutation = new int[10];
for (int i = 0; i < myArray2.length - 1; i++) {
int ranIndex = (int) (Math.random() * 10 - 1) + 1;
if (myArray2[ranIndex] == 0) {
ranIndex = (int) (Math.random() * 9);
}
if (myArray2[ranIndex] > 0) {
permutation[i] = myArray2[ranIndex];
myArray2[ranIndex] = 0;
}
}
System.out.println(Arrays.toString(permutation));