I want to generate random numbers, but don't want them to be from exclude
array. Here is my code.
public int generateRandom(int start, int end, ArrayList<Integer> exclude) {
Random rand = new Random();
int range = end - start +1 - exclude.size();
int random = rand.nextInt(range) + 1;
for(int i = 0; i < exclude.size(); i++) {
if(exclude.get(i) > random) {
return random;
}
random++;
}
return random;
}
I use this function in a while loop, and during each iteration I add a new value to exclude
.
Sometimes it returns numbers that belong to exclude
. What's the problem?