Implementation:
private static List<Integer> getRandomDistribution(List<String> unsortedList, int max) {
Random random = new Random();
List<Integer> indexContainer = new ArrayList<>();
for (int i = 0; i < max; i++) {
int index = random.nextInt(max);
// Below is what I don't like,
if (indexContainer.contains(index)) {
i--;
} else {
indexContainer.add(index);
}
}
return indexContainer;
}
So basically its saying that, until I don't find the required unique random number. I will continue the loop, what could happen is it might keep looping for a long time thus increasing the overhead.
Problems:
- int index = random.next(max) is what should decide the randomness, also I will have to maintain the ordering. That why I have used List
- Secondly, i-- is where I am stuck, because frankly I don't like the implementation.
NOTE: I will also have to maintain the order within the indexContainer.