How to make my random questions not repeat?

0

Issue

I’m learning to program, i need my quiz random questions not to be repeated.
Java – Android Studio

##my code:

quizModalArrayList = new ArrayList<>();
random = new Random();
    
getQuizQuestion(quizModalArrayList);
currentPos = random.nextInt(quizModalArrayList.size());
    
private void getQuizQuestion(ArrayList<QuizModal> quizModalArrayList) {
    
quizModalArrayList.add(new QuizModal(R.drawable.prolinfocitos, "b", "b", "c", "dd","b"));
quizModalArrayList.add(new QuizModal(R.drawable.prolinfocitos, "b", "b", "c", "dd","b"));

quizModal
there are more lines of code, but I believe they are unrelated to the problem.

any help?
Thanks

Solution

You have to rethink your approach. The easiest solution: you define the complete random order upfront. Meaning: you first load all your questions into your base list, in whatever order. Then you create a copy of that list, and shuffle that copy (using that built in function: Collections.shuffle(List). And that shuffled list copy is what your user gets to see. And then you just iterate that list bottom to top.

Answered By – GhostCat

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More