-2

The goal of this program is to have A2[] display the numbers 1-10 in a random order.It needs to be done without anything past base level knowledge. A2[] gets the numbers from A1[], the A1[] array has the numbers 1-10 stored in sequence. In its current state the program runs, but does not filter out results that have already been store in A2[]. For example.... 4,2,3,7,5,9,7,1,4 should not be able to be a result. Only a random order of 1-10 should display, with each int occurring only once. Any help is greatly appreciated. The code presently is as follows : `

public class W07problem07 {

    public static int getRandomIntRange(int min, int max) {
        int x = (int) (Math.random() * ((max - min))) + min;
        return x;
    }

    public static void main(String[] args) {
        int ranNum;
        int count = 1;
        int[] A1 = new int[10];
        int[] A2 = new int[10];

        //loop for storing 1-10 int number withing  A1[].
        for (int k = 0; k < A1.length; k++) {
            A1[k] = count;
            count++;
        }
        for (int k = 0; k < A2.length; k++) {
            A2[k] = k;
        }

        for (int j = 0; j < A2.length; j++) {
            int a;
            ranNum = getRandomIntRange(0, A2.length);
            a = A2[j];
            if(a==ranNum){
                j--;
            } else{
                A2[j]= A1[ranNum];
            }
        }



        for (int k = 0; k < A2.length; k++) {
            System.out.println(A2[k]);
        }
    }
}
`
Mr.Pat
  • 3
  • 4
  • Possible duplicate of [Random shuffling of an array](https://stackoverflow.com/questions/1519736/random-shuffling-of-an-array) – Zephyr Mar 12 '19 at 20:56
  • Its similar, but I can only use A1[] and A2[]. Its a Hw for class and we have not gone over anything like whats in that thread yet. – Mr.Pat Mar 12 '19 at 21:03
  • Since your source array has only the numbers from 1-10, you can fill the target array with numbers outside this range, like -1 (this is called a sentinel value). Then, for each number in the source array, pick a random target array index and check to see if it is available (has -1 in it). Keep picking a new index until you find an available slot. – Idle_Mind Mar 12 '19 at 21:16
  • ohhh!!! I had not thought of that. I will try it out now and see how it goes. Thanks! – Mr.Pat Mar 12 '19 at 21:21

2 Answers2

1

There are really many ways to do what you are asking.

One of the simplest is appling the hash theory, using the next uncalled number as next (pseudo)random number.
I'm assuming that what you wrote in your code doesn't make much sense, so to follow what's next, i'm assuming A1 is populated with numbers 1-10 and you are populating A2 directly with shuffled numbers.

Example:
this is the current state of your A2. The next random number picked is 1, but that is not good as long it was already picked.

A1   |1 2 3 4 5 6 7 8 9 10|
A2   |1 4 7 9 . . . . . . |

So when a "collision" is found continue to apply this fix:
the new random number become x = x+1 until an unpicked number is taken.

So

A1   |1 2 3 4 5 6 7 8 9 10|
A2   |1 4 7 9 2 . . . . . |

As you can imagine there are a lot of collision policy that you can use, the previous one is called open addressing.

Zartof
  • 185
  • 1
  • 12
0

One more solution

public class W07problem07 {
    public static int getRandomIntRange(int min, int max) {
        int x = (int) (Math.random() * ((max - min))) + min;
        return x;
    }
    public static void main(String[] args) {
        int count = 1;
        int[] A1 = new int[10];
        int[] A2 = new int[10];
        //loop for storing 1-10 int number withing  A1[].
        for (int k = 0; k < A1.length; k++) {
            A1[k] = count;
            count++;
        }
        int j = 0;
        while (j < A1.length) {
            int ranNum = getRandomIntRange(1, A1.length + 1);
            if (!numAlreadyExists(A2, ranNum)) {
                A2[j++] = ranNum;
            }
        }
        for (int k = 0; k < A2.length; k++) {
            System.out.println(A2[k]);
        }
    }
    public static boolean numAlreadyExists(int[] array, int element) {
        for (int i = 0; i < array.length; i++) {
            if (element == array[i]) {
                return true;
            }
        }
        return false;
    }
}
  • This might output the desired output for now. But in your code you are not using the `A1` array. If `A1` contains for example numbers from 11 to 20 your solution will still print the numbers 1 to 10. – Eritrean Mar 12 '19 at 23:11