1

So I am trying to make a program that produces an array of 20 random numbers, that does not have duplicates (to the end user). Here is my code so far

import java.util.*;
public class randomprog
{
public static void main(String args[])     
{
    Random rand = new Random();
    int[] list = new int[20];
    boolean generating=true;
    int counting=0;
    while(generating)
    {
        int testNum= rand.nextInt(30)+1;
        if (Arrays.asList(list).contains(testNum))
        {}
        else
        {
            list[counting]=testNum;
            counting++;
            System.out.println(testNum);
        }
        if(counting>=20)
        {
            generating=false;
        }
    }
}}

So as you can see I have already tried using Arrays.asList(list).contains(mynumber) however I still recieve duplicates in my output like 29 4 4 1 20 30 20 23 30 11 6 7 27 14 16 8 4 19 7 15

Any suggestions?

Nick Rudd
  • 11
  • 1
  • 3

4 Answers4

4

Use a HashSet to keep track of the numbers you have used.

For example

int[] result = new int[20];

HashSet<Integer> used = new HashSet<Integer>();
for (int i = 0; i < 20; i++) {
    int add = (int)(Math.random() * 30); //this is the int we are adding
    while (used.contains(add)) { //while we have already used the number
        add = (int) (Math.random() * 30); //generate a new one because it's already used
    }
    //by this time, add will be unique
    used.add(add);
    result[i] = add;
}

This ensures that you will have no duplicates, and is also much faster than searching in an ArrayList, which will perform a number of operations equivalent to the size of the ArrayList each time you search for a number. A HashSet only performs 1 operation when you check if a number is contained.

  • it is good to know that, but with a quad core cpu an arraylist of 20 Integers should also be fine in my opinion – Theo Feb 27 '16 at 21:17
1

The reason why your code doesn't work is that Arrays.asList(int[] list) returns an ArrayList<int[]> of size 1, and not an ArrayList<Integer>. So when you call contains, it's not checking against the integer elements of the original list, and is always returning false.

Maljam
  • 6,244
  • 3
  • 17
  • 30
0

I would recommend using an ArrayList and do not use empty if blocks. This should work.

   import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;

    public class Main {
            static List list = new ArrayList<>(20);
            public static void main(String args[])
            {
                Random rand = new Random();
                boolean generating=true;
                int counting=0;
                while(generating)
                {
                    int testNum= rand.nextInt(30)+1;
                    if (!list.contains(testNum))
                    {
                        list.add(testNum);
                        counting++;
                        System.out.println(testNum);
                    }
                    if(counting>=20)
                    {
                        generating=false;
                    }
                }
            }
}
Theo
  • 1,165
  • 9
  • 14
0

Use Collections.Shuffle

class Ideone{
    public static void main(String[] args) {
    Integer[] arr = new Integer[20];
    for (int i = 0; i < arr.length; i++) {
        arr[i] = i;
    }
    Collections.shuffle(Arrays.asList(arr));
    System.out.println(Arrays.toString(arr));

}}
FallAndLearn
  • 4,035
  • 1
  • 18
  • 24