0

I've created an ArrayList for integers which I would like to fill with 200 numbers. Each number can be within a range between 0 and 1023.

Therefore I've written this code:

Random rand = new Random();
ArrayList<Integer> values = new ArrayList<Integer>();

int START_AMOUNT = 200;

for(int i = 0; i < START_AMOUNT; 

  values.add(rand.nextInt(1024));      

}

As You might see, the for-loop will add 200 random numbers to the "values" ArrayList, from 0 to 1023. Now my problem is that I want the Array to have only unique numbers. How can I tell the Random class not to generate any numbers that already are existent in the ArrayList?

3

4 Answers 4

4

What I'd do is creating an array of 1023 int composed by 1,2,3,...,1023. Then you shuffle it, and you take only the 200 first terms :

List<Integer> ints = new ArrayList<Integer>();
for(int i = 1; i <= 1023; i++)
{
    ints.add(i);
}

Collections.shuffle(ints);

EDIT as suggested by @Bohemian♦

List<Integer> result = ints.subList(0,200);
Sign up to request clarification or add additional context in comments.

3 Comments

Lose everything after the first loop and use List.subList() instead: ints = ints.subList(0, START_AMOUNT);
I didn't want to make mistake because I can't remember well Lists manipulation. So this works and he have the idea, but yes sublist seems pretty nice.
Pretty nice idea, I remember having it done similiar to this, thanks!
2
A Set is a Collection that cannot contain duplicate elements. 
It models the mathematical set abstraction. 
The Set interface contains only methods inherited from Collection 
and adds the restriction that duplicate elements are prohibited. 

And therefore,

public boolean add(E e)
  Adds the specified element to this set if it is not already present. 
  [...]
  If this set already contains the element, 
      the call leaves the set unchanged and returns false.

As such, what I'd do is use a Set, then add those to the list:

List<Integer> values = new ArrayList<Integer>();
Set<Integer> set = new HashSet<Integer>();
while(set.size() < 200) 
{
    set.add(rand.nextInt(1024)); 
}
values.addAll(set);

1 Comment

I agree, you should use a set to avoid duplication.
1

Use a Set:

Random rand = new Random();
Set<Integer> values = new HashSet<Integer>();
final int START_AMOUNT = 200;

while(values.size() < START_AMOUNT) { 
    values.add(rand.nextInt(1024));      
}
List<Integer> uniqueList = new ArrayList<Integer>(values);
System.out.println(uniqueList);

4 Comments

I was faster by 7 seconds, heheh.
What's the difference between using a Set or another List?
When you try to add an element into the Set using add() that is already contained by the Set, then the add() method will return false and not add the duplicate element.
See Zhuinden answer above. A Set cannot contain duplicates.
0

You could also check if the ArrayList contains the given random number everytime you want to add one.

Random rand = new Random();
Integer r;
ArrayList<Integer> values = new ArrayList<Integer>();

int START_AMOUNT = 200;

for(int i = 0; i < START_AMOUNT; i++) {

  r = rand.nextInt(1024);
  If !values.contains(r) {
    values.add(r);
  } else {
   i--;
  }

}

Although i think Kabulan0lak's answer would be more performant if that is important.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.