Random number generation, but it should be generated with unique numbers without repetition

int[] drawNumbers = new int[10];//Array With 10 Random Numbers USED for DRAWN NUMBERS
String x = "Drawn Numbers: ";
List<Ticket> ticketWon ;

do{
    //GENERATING 10 Random Numbers
    for (int i = 0; i <= drawNumbers.length -1 ; i++) {
           Random r = new Random();
           drawNumbers[i] = r.nextInt(99) + 1;
           x += drawNumbers[i] + " ";
    }
}

I am trying to create 10 random numbers that should be random and unique. My problem is that when Random r = new Random()duplicate numbers appear. How can I generate 10 random numbers from a range of 1 to 99 without repetition?

The problem for the lottery system

I would like to use Collection.Shuffle, but I'm not sure how this should be implemented.

+4
source share
5 answers

Here is an alternative way to achieve the desired result. We fill the list with values ​​from 1 to 99. Then we shuffle the list and get the first 10 values:

public static void main(String[] args) {
    ArrayList<Integer> list = new ArrayList<Integer>();
    for (int i=1; i<100; i++) {
        list.add(new Integer(i));
    }
    Collections.shuffle(list);
    for (int i=0; i<10; i++) {
        System.out.println(list.get(i));
    }
}

/ Random, . , @Voicu ( ), shuffle :

public static void shuffle(List<?> list) {
    if (r == null) { 
        r = new Random();
    }
    shuffle(list, r);
}
private static Random r;
+5

Set, 10 . , , .

final int NUMBERS_TO_DRAW = 10;
Random r = new Random();

// Generate NUMBERS_TO_DRAW random numbers 
Set<Integer> randoms = new HashSet<>();
while (randoms.size() < NUMBERS_TO_DRAW) {
    randoms.add(r.nextInt(99) + 1);
}

// Now shuffle them:
List<Integer> shuffledRandom = new ArrayList<>(randoms);
Collections.shuffle(shuffledRandom);

EDIT:
@MarkPeters , LinkedHashSet :

final int NUMBERS_TO_DRAW = 10;
Random r = new Random();

// Generate NUMBERS_TO_DRAW random numbers 
LinkedHashSet<Integer> randoms = new LinkedHashSet<>();
while (randoms.size() < NUMBERS_TO_DRAW) {
    randoms.add(r.nextInt(99) + 1);
}
+1

, 1 99, 10 . , .

1 99 - :

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

Then we need to choose a random card between the 0th card (lists are numbered starting from 0) and the number of elements in the list:

int draw = r.nextRandom( deck.size() );
Integer card = deck.remove( draw );

And repeat this 10 times, doing something with the β€œmap” (say, putting it in an array or another list, or something else:

int drawNumbers = new int[10];
for( int co=0; co<10; co++ ){
  int draw = r.nextRandom( deck.size() );
  Integer card = deck.remove( draw );
  drawNumbers[co] = card;
}
+1
source

Use Set<Integer>.

Set<Integer> set = new HashSet<Integer>();
int[] drawNumbers = new int[10];
Random r = new Random();
for(int i=0; i<10; i++)
{
    drawNumbers[i] = r.nextInt(99) + 1;
    while(set.contains(drawNumbers[i]))
        drawNumbers[i] = r.nextInt(99) + 1;
    set.add(drawNumbers[i]);
}
+1
source

It would be easier to use listso that you can check if it already contains a number and regenerate if it does.

List<Integer> drawNumbers = new ArrayList<Integer>();
Random r = new Random();
int newNumber = -1;
do
{
    newNumber = r.nextInt(99) + 1;
} while(drawNumbers.contains(newNumber); //Make sure the number is not already in the list.

Then put this in a loop to repeat 10 times.

0
source

All Articles