I wanna create a random integer without duplicates when the max list is one length from my list.
I use this code but I have duplicates. What I should change?
Random rand = new Random();
n = rand.nextInt(QuestionLibrary.mChoices.length) + 1;
I wanna create a random integer without duplicates when the max list is one length from my list.
I use this code but I have duplicates. What I should change?
Random rand = new Random();
n = rand.nextInt(QuestionLibrary.mChoices.length) + 1;
The solution outlined below is more efficient than checking that each random number hasn't been picked already.
The idea is that you populate a list with all possible numbers in your random range. Then, you shuffle the list. You get each new random number by subsequently popping items from the front of the shuffled list.