2
\$\begingroup\$

I've coded a simple quiz game for Android, and currently Im having troubles with making questions not appear after they've been shown, i.o. I dont want it to ask me the same question twice..

This is the method Im using

 private void QBegin() {
   /*
    * Gets a random question
    */
    question = (TextView) findViewById(R.id.question);
    String[] types = { "Question one",
            "Question two", 
            "Question three", 
            "Question four",
            "Question five"};
    Random random = new Random();
    int qType = random.nextInt(types.length);
    question.setText(types[qType]);
    getAnswers(qType); //gets four answers
}

Im not sure if this will work but, what if I add something like

Edit : Doesn't work..

    int i = 0;
    ArrayList<Integer> list = new ArrayList<Integer>();
    list.add(qType);
    i++;
    if(list.contains(qType) && i != types.length + 1){
        return;
    } else {
        answerCounter.setText("Hit the bricks pal, you're done..");
    }

On http://stackoverflow.com got told to , add smth like:

    ArrayList<Integer> list = new ArrayList<Integer>();
    list.add(types.length);
    Collections.shuffle(list);
    if(!list.contains(qType)){
        // help please, as I have no idea on what I should be doing!
    }
\$\endgroup\$

closed as off-topic by Jamal Jun 29 '14 at 3:36

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. Such questions may be suitable for Stack Overflow or Programmers. After the question has been edited to contain working code, we will consider reopening it." – Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.

6
\$\begingroup\$

There are basically two different ways of doing this efficiently:

  • Put the questions in a list
  • Shuffle the list
  • Loop through the list and show the questions

or:

  • Put the questions in a list
  • Pick a question to show by random
  • Remove that question from the list
  • Repeat until the list is empty
\$\endgroup\$
4
\$\begingroup\$

The usual way is to start with something like an array of indexes, then shuffle those into (pseudo-) random order with something like the Fisher-Yates shuffle.

Another possibility is to use a random number generator whose range and period are exactly equal to the number of entries you need to shuffle. Offhand I don't remember any algorithms for finding a generator with a specified range and period though.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.