Just wanted other opinions on my code and a way to figure out whether or not each number placed into the 2D array is unique. My mind is blanking out a bit, I placed something in the method before but erased it. Debating on whether the parameters should simply be the 2D array by itself, or the array and the temporary number.

Recommended Answers

All 2 Replies

Place numbers 1-16 in an ArrayList:

        //holds numbers 1-16
        ArrayList uniqueNums = new ArrayList();

        //add numbers 1-16
        for (int i=0; i < 16; i++)
        {
            uniqueNums.add(i+1);
        }//for

Use the following random number generator method. It was taken from here.

randInt:

    public static int randInt(int min, int max) {

        // Usually this can be a field rather than a method variable
        Random rand = new Random();

        // nextInt is normally exclusive of the top value,
        // so add 1 to make it inclusive
        int randomNum = rand.nextInt((max - min) + 1) + min;

        return randomNum;
    }

"randInt" is used below.
Generate unique random numbers like this:

        //choose random int
        for (int j = 15; j >= 0; j--)
        {
            int selectedNum = randInt(0,j);

            //i print out the selected number.
            //you will add it to your matrix here
            System.out.println("selected:  " + uniqueNums.get(selectedNum));

            //after a number is added to the
            //matrix, remove it from
            //the arraylist
            uniqueNums.remove(selectedNum);

            System.out.println();
        }//for

You can add your numbers to the matrix where I have the "System.out.println...." statement.

Thanks a lot, I'll definitely make note of this whenever I run into the problem again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.