1

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

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
  • 1
    Would a random shuffle help? https://stackoverflow.com/questions/1519736/random-shuffling-of-an-array – doctorlove Jan 03 '22 at 12:28
  • 3
    In order to prevent questions to show up mutliple times, you need to track which questions already have been seen. – MC Emperor Jan 03 '22 at 12:28

1 Answers1

3

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.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Thanks for the "reminder", wanted to edit the answer in that respect, and forgot about it ;-) – GhostCat Jan 03 '22 at 12:58
  • Get all questions at the start and create a copy -> shuffle copy and show a question to the user -> remove the seen question from copy -> shuffle again and show the question. repeat until you reach the end of copied list. – Faiizii Awan Jan 03 '22 at 13:11