dimanche 22 janvier 2017

How to generate a list of distinct random numbers including a specific number in JAVA?

Ok, so the scenario is, I want to generate a list of 4 distinct random numbers which will represent 4 random choices for a quiz application. One of the 4 random choices will be the correct answer, so we will already know the index of the correct choice. This correct index or number must be included in the random number list.

For example: Consider that we have an array of length 100, containing string values representing 100 choices for a question, and the index of correct choice is 45. Now we want 4 random choices for this question including the index 45, so that the index list will be something like {2, 91, 45, 17}. Also the list shouldn't contain duplicate numbers.

Any idea how to achieve this in Java ?




Aucun commentaire:

Enregistrer un commentaire