vendredi 27 février 2015

How to have seven unique number between 0 and 14?

I need to have seven unique number between 0 and 14 (from 0 to 13). I came up with the below code -



private Set<Integer> generateNumber() {
Set<Integer> randomNumberList = new HashSet<Integer>();

while (true) {
if (randomNumberList.size() == 7) {
break;
}
randomNumberList.add(random.nextInt((14 - 0) + 1) + 0);
}

return randomNumberList;
}


Is there any better way of doing this? I am not sure whether I should use while true here for this simple thing.





Aucun commentaire:

Enregistrer un commentaire