desperateStudent
desperateStudent

Reputation: 81

How to randomly pick element from array without repeating?

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);
            }
        }
    });

Upvotes: 1

Views: 2629

Answers (4)

MC Emperor
MC Emperor

Reputation: 23037

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

Upvotes: 2

Ridcully
Ridcully

Reputation: 23665

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 ... 

Upvotes: 1

Jagruttam Panchal
Jagruttam Panchal

Reputation: 3210

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!

Upvotes: 0

Sasi Kumar
Sasi Kumar

Reputation: 13358

    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)

Upvotes: 1

Related Questions