Reputation: 3114
Hi
consider that I have a for
loop : for(int i = 0;i<4;i++)
in the for
loop ,I want to print the random of numbers from 0 till 3 and the result include just 3 numbers from this.and each time that for loop execute ,the result of math.random must be different i.e.,
I have 4 numbers : (1,2,3,4)
and I want to have 4 results after for loop executes: [1,2,3] [1,2,4] [2,3,4] [4,1,3]
how can i produce these numbers?
thanks
Upvotes: 0
Views: 460
Reputation: 47183
Here's an idea:
Collections.shuffle
Step 4. is the one where you will get all your elements randomly, without duplication.
Note: in step 3., you can also create a new list from the master list instead of a stack if it seems easier, but the basic premise is the same.
Upvotes: 3