on the first iteration- I want to create a random number from 0..10
This is how I’ve implement it-
int generator= (int)(Math.random() * (10));
On the first iteration I want to create another random number, but without one of the values.
For example I would like the random number without the number 4.
only 1,2,3,5,6,7,8,9,0
How can I remove one of the values from the numbers 0..10 when I generate a random number?
1st Way: –
You can also maintain a
Listof all the numbers. Then useCollections.shuffleto shuffle the list and get the first element. And remove it.OUTPUT: –
2nd Way: –
A better way would be to generate a random number from
0to thesizeof the list, and get the value ofthat indexand remove it.NOTE: –
If you want to exclude a fixed set of numbers from being generated, then you can have those numbers in a list. And then remove all elements of that list from your
list of numbers. And then use any of the approach above.