Reputation: 81
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
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);
}
}
==
to compare String
s. Never ever do that. Use equals()
instead. See How do I compare strings in Java?.Answer
. According to the Java Naming Conventions, variables should start with a lowercase character. See Code Conventions at the Oracle website.Question
class, because it holds the answer string. See An Object Must be Responsible for Itself.Upvotes: 2
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
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
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