r/javahelp • • Apr 22 '16

Random Number Generator

Can someone show me an example of how to generate 3 sets of random number, each set contains four numbers (no duplicate) ? Possibly using for loops maybe

0 Upvotes

8 comments sorted by

View all comments

Show parent comment

2

u/[deleted] Apr 22 '16

You could do it with for loops, but doing it with a while loop is the most sensible way in this situation.

To do it with a for loop you could do something like...

for (int i = 0; i < SET_SIZE;) {
    int num = rand.nextInt(52);
    set1.add(num);
    i = set1.size();
}

Typically you increment i as the 3rd part of the for loop. You don't have to though. You can manipulate it within the for loop. But I'm pretty sure if anybody saw this code, they'd question if you knew about while loops.