7

I want to create 10 random numbers in the range 0-500. But the problem is that I want those numbers to be unique. For 2 random numbers i could create something as the following:

int randomItem1 = r.nextInt(500);
int randomItem2 = r.nextInt(500);
while(randomItem1==randomItem2){
    randomItem1=randomItem();
    randomItem2=randomItem();
}

But if I do this for 10, I think that the while it will stack. And I'm saying this because I'm trying to create a huge algorithm which is trying to make continuous evaluations and i want continously to take 10 random and unique numbers. I don't know what to do. Any ideas or suggestions?

S.K. Venkat
  • 1,749
  • 2
  • 23
  • 35
Alex Mellon
  • 105
  • 1
  • 1
  • 5
  • 1
    Try google searching it first http://www.wikihow.com/Generate-N-Different-Random-Numbers – Eran Egozi Feb 24 '12 at 00:18
  • 2
    But that's a terrible site and the answer there is wrong. – BCoates Feb 24 '12 at 00:31
  • @MichaelT Doubt it. The other one wants *one* number in the range, while this one wants *multiple* numbers in the range. – Dennis Meng Jan 13 '14 at 07:47
  • @DennisMeng Apparently I misflagged when I did the dup (open windows and all that). I was actually trying to do [Generate a set of unique numbers java](http://stackoverflow.com/questions/10678107/generate-a-set-of-unique-numbers-java) to this one (which it is), and forgot to clean this one up. Thank you for reminding me. –  Jan 14 '14 at 15:55
  • possible duplicate of [Generating Unique Random Numbers in Java](http://stackoverflow.com/questions/8115722/generating-unique-random-numbers-in-java) – Alex - GlassEditor.com Jul 28 '15 at 18:31

4 Answers4

18

Looks like you are storing these in individual variables. The "normal" place to store groups of items like this would usually be in a list or array.

In this case, store them in a "set" data structure instead. It will not allow duplicates.

Set documentation: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Set.html

Set set = new HashSet();

while (set.size() < 10) {
    set.add(r.nextInt(500));
}
Chris Dutrow
  • 48,402
  • 65
  • 188
  • 258
5

Java Collections has a shuffle method. You can put your numbers into an ArrayList and then shuffle its content. If the ArrayList contains n numbers, calling the shuffle method, would give you the same ArrayList containing n numbers but arranged randomly.

for(int i=0;i<10;i++){
list.add(i);  // list contains: [0,1,2,3,4,5,6,7,8,9]
}
Collections.shuffle(list);// list now contains: [0, 9, 3, 1, 5, 8, 7, 2, 6, 4]
Muhammad Asaduzzaman
  • 1,201
  • 3
  • 19
  • 33
3

Make a LinkedList of the numbers from 1-500 and shuffle one out of them each time you use a number using The Fisher-Yates shuffle.

This will give you guaranteed sane (constant time) performance for each number pulled.

BCoates
  • 1,508
  • 10
  • 16
-1

I would use an array, and store the numbers as they're generated into that array. You would generate a new random, then need to iterate through your array up to your number count, checking to see if it matched any you have previously created.

  • That would be an awful design. Indeterminate CPU needs. The previous replies are much more to the point : create a source set of eligible numbers, "shuffle them" and then just iterate the resulting random list. – RichieHH May 19 '14 at 11:21
  • For this solution, I feel like the main problem would be writing more code and not using readily available data structures. Because you would not be using a readily available data structure such as a set, you would spend more time writing and debugging the code. Depending on your application, I'm not sure I'd worry so much about CPU needs considering the OP only needs 10 random numbers. However, a lot of the readily available "set" data structures would also be designed use something more efficient that the O(n^2) algorithm you suggest above. – Chris Dutrow Feb 12 '15 at 00:38