user3681327
user3681327

Reputation: 39

Generating random Numbers , but it must be Generated with Unique numbers without replications

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'm trying to generate 10 random numbers that must be random generated and Unique. My problem is that with Random r = new Random() there are times that replicated numbers are shown. How can i generate 10 random numbers from range 1 to 99 without replications?

Problem is for a Lottery System

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

Upvotes: 4

Views: 546

Answers (5)

benscabbia
benscabbia

Reputation: 18072

Here is an alternative way to achieve your desired result. We populate a list with values 1 to 99. Then we shuffle the list and grab 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));
    }
}

You won't have to import/directly deal with Random, which is a plus. However, as pointed out by @Voicu (in the comments), shuffle does indeed utilize random:

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

Upvotes: 5

Mureinik
Mureinik

Reputation: 311228

I'd generate random numbers and save them to a Set until I get 10 numbers. Then create a list from it, shuffle it, and then take numbers from it.

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:
As @MarkPeters noted in the comments, using a LinkedHashSet would eliminate the need to shuffle:

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);
}

Upvotes: 1

HelloWorld
HelloWorld

Reputation: 1103

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]);
}

Upvotes: 1

Prisoner
Prisoner

Reputation: 50701

You can broadly think of this problem as having a deck of cards numbered from 1 to 99, and you want to pick 10 of those cards. The solution would be to, programmatically, create that deck and then randomly select from that deck and remove from the deck.

We can model the deck as a List of Integers and populate that List with entries from 1 to 99 with something like this:

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

We then need to pick a random card between the 0th card (lists are numbered starting at 0) and the number of elements in the list:

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

And repeat that 10 times, doing something with the "card" (say, putting it into an array, or another list, or whatever:

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;
}

Upvotes: 1

Jean-Fran&#231;ois Savard
Jean-Fran&#231;ois Savard

Reputation: 21004

It would be easier to use a list so you could check if it already contains the number and re-generate 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.

Upvotes: 0

Related Questions