-1

I have a method that I used it to take random number between 5 and 10 but the problem there is a duplication, I want to change the code in a way that I can get random number between 5 and 10 without duplication.

here is my code:

    int min = 5;
    int max = 10;

    Random r = new Random();
    int random_number = r.nextInt(max - min + 1) + min;
user2420263
  • 157
  • 1
  • 4
  • 15
  • possible duplicate of [Java - generate Random range of specific numbers without duplication of those numbers - how to?](http://stackoverflow.com/questions/5224877/java-generate-random-range-of-specific-numbers-without-duplication-of-those-nu) – Henry Jan 17 '15 at 06:41

1 Answers1

0

Take randomly from a known collection until it is empty.

List<Integer> ints = new ArrayList<Integer>(Arrays.asList(6, 7, 8, 9, 10));
Random r = new Random();
while (ints.size() > 0) {
    System.out.println(ints.remove(r.nextInt(ints.size())));
}

If necessary, you can start again with a new list when it is empty and keep going forever.

Synesso
  • 37,610
  • 35
  • 136
  • 207