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?

user2410644
  • 3,861
  • 6
  • 39
  • 59
  • 1
    possible duplicate of [Generating Unique Random Numbers in Java](http://stackoverflow.com/questions/8115722/generating-unique-random-numbers-in-java) – Parimal Raj Jul 29 '14 at 09:27
  • http://stackoverflow.com/questions/4040001/creating-random-numbers-with-no-duplicates – nobalG Jul 29 '14 at 09:34
  • you don't tell the `Random` class to do anything - that would make it non-random. – Alnitak Jul 29 '14 at 09:38

4 Answers4

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);
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
Kabulan0lak
  • 2,116
  • 1
  • 19
  • 34
  • 3
    Lose everything after the first loop and use List.subList() instead: `ints = ints.subList(0, START_AMOUNT);` – Bohemian Jul 29 '14 at 09:39
  • 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. – Kabulan0lak Jul 29 '14 at 09:40
  • Pretty nice idea, I remember having it done similiar to this, thanks! – user2410644 Jul 29 '14 at 09:41
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);
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
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);
JamesB
  • 7,774
  • 2
  • 22
  • 21
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.

ch1ll
  • 419
  • 7
  • 20