There are several ways of doing this, such as:

  • If there aren't many numbers: fill an ArrayList with the numbers (ie all the numbers from 1 to 10) and call the Collections.shuffle() method to randomise the list. Finally pull out the first 'n' numbers.
  • Create a Set and fill it with random numbers until it's size is the required size (ie 6), then use an iterator to extract the numbers.
  • Or, if you really have to use an array, check the number is not already in the array before you add it.