1

I am working on an quiz app. I managed to randomized the questions(from an array) with

int position = new Random().nextInt(questions.length);

but the questions keep on repeating. How do i make it stop getting when it reaches lets say 10 questions without repeating? Here is my code if it helps:

gcfQuiz.setText(questions[position]);

    gcfButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            EditText Answer = (EditText) findViewById(R.id.txtAns1);
            String finAns = Answer.getText().toString();

            if (finAns==answers[position]){

                correct++;
            }
            position++;
            if (position<questions.length)
            {
                gcfQuiz.setText(questions[position]);

            }else {
                Intent in = new Intent(getApplicationContext(), gcfResult.class);
                startActivity(in);
            }
        }
    });
desperateStudent
  • 81
  • 1
  • 1
  • 8

4 Answers4

2

You could use a List instead, and then remove the element if it is chosen:

List<Question> questions = new ArrayList<Question>();
// For Question class, see below.

// Get some random valid index from the list.
int index = new Random().nextInt(questions.size());
// Remove the question from the list and store it into a variable.
Question currQuestion = questions.remove(i);

For your information, I assumed the Question class looks like this. This approach is generally better than two separate arrays with questions and answers respectively:

class Question {

    private String text;
    private String answer;

    public Question(String text, String answer) {
        this.text = text;
        this.answer = answer;
    }

    public boolean isCorrectAnswer(String inputAnswer) {
        return Objects.equals(answer, inputAnswer);
    }
}

Some tips

Community
  • 1
  • 1
MC Emperor
  • 22,334
  • 15
  • 80
  • 130
  • My questions and answers arrays are all in the same class as the code. Will it be better if it was in a seperate class? Thanks for answering – desperateStudent Jan 05 '17 at 10:10
  • Yes, that's the whole [idea of object-oriented programming](https://www.cs.drexel.edu/~introcs/Fa15/notes/06.1_OOP/Advantages.html?CurrentSlide=3), especially encapsulation: you put things that logically belongs to eachother in a class. Each question has a correct answer, and you transfer the responsibility of checking whether the answer is correct or not to the question. – MC Emperor Jan 05 '17 at 10:16
1
    int n=questions.length;
//if n=100 means it give random numb with no duplicate values with in the range 100.
    Random r = new Random();
    Set<Integer> positionvalue = new HashSet<>();
    for(int i = 0; i <n; i++){
            while(true) {
            int number = r.nextInt(n) + 1;
            if (positionvalue.contains(number) == false) {
                positionvalue.add(number);                   
                break;
            }
        }
    }

positionvalue set have non-repeating random number between your range(n)

sasikumar
  • 12,540
  • 3
  • 28
  • 48
1

Create a list of questions and shuffle it (via Collections.shuffle()) one time to get a random order of questions:

List<String> randomQuestionList = new ArrayList<>();
randomQuestionList.addAll(Arrays.asList(questions));
Collections.shuffle(randomQuestionList);

Now you have a randomly ordered list of your questions.

Note I saw in your code, that you have your answers in a separate array. To make this shuffle solution work for you, you will need to hold question and answers together. The best way to achieve that, would probably be to create a Question class that holds the question and the answers. Then you can shuffle the questions and still have the answers to every question right at hand.

class Question {
    String question;
    String[] answers;
}

List<Question> randomQuestionList ... 
Ridcully
  • 23,362
  • 7
  • 71
  • 86
  • how do i know if the answer is correct tho? Thanks for answering – desperateStudent Jan 05 '17 at 10:29
  • You should know, which of your answers is the correct one :-) You could add a field to Question class with the index of the correct answer or create an Answer class with a text and a right/wrong flag (this would also handle cases where you can have more than 1 correct answer). – Ridcully Jan 05 '17 at 10:32
  • Note, that you should probably display the answers in a random order as well -- you can again use shuffle() for that :-) – Ridcully Jan 05 '17 at 10:33
0

Take two global variables

int nextQuestion;
List<Integer> posList;

Then initialise it:

private void initRandomLogic() {
    nextQuestion = 0;
    posList  = new ArrayList<>();
    for(int i=0;i<questions.length;i++)
        posList.add(i);
    Collections.shuffle(posList);
}

private int getRandomPosition() {
    return posList.get(nextQuestion++);
}

Hope will help you!

Jagruttam Panchal
  • 3,152
  • 2
  • 15
  • 21