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?

0

4 Answers 4

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.

1
  • 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, 2016 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.

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;
                    }
                }
            }
}
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));

}}
6
  • 1
    How will shuffling an array remove duplicates? Isn't shuffling just randomizing the order? Feb 27, 2016 at 21:10
  • He asked "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)". Feb 27, 2016 at 21:13
  • 1
    Shuffling does not remove duplicates. Feb 27, 2016 at 21:15
  • The array will then contain all integers from 0 to 19 in random order. So yeah, that may well be what is asked for.
    – til_b
    Feb 27, 2016 at 21:17
  • I agree with you. But he asked that he need to create array of 20 random numbers which does not repeat themselves. Above code snippet will do the work. Feb 27, 2016 at 21:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.