I got a problem in generating randomly. I have a set of questions that are stored in a binary file. I have made a piece of code where these questions, when called from the binary file, are generated randomly. But i don't know why, some of the questions are repeating themselves. What do i have wrong? Thanks a lot in advance. :-)
This is the code:
This is the code:
Code:
public void askQuestions(String[] questions, String[] answers) {
int count = 0;
int point = 0;
int[] A = new int[questions.length];
int[] B = new int[A.length];
int countA = A.length;
int countB = 0;
Random generator = new Random();
for (int i = 0; i < A.length; i++){
A[i] = i;
}
while (countA > 0){
int pos = generator.nextInt(A.length);
B[countB] = A[pos];
countB++;
for (int i = pos; i < A.length - 1; i++){
A[i] = A[i + 1];
}
countA--;
}
for(int j = 0; j < questions.length; j++) {
timeForMore = true;
int randomIndex = B[j];
String input = JOptionPane.showInputDialog(null, questions[randomIndex]);
if(answers[randomIndex].equalsIgnoreCase(input))
count++;
point++;
if(!timeForMore)
break;
}
JOptionPane.showMessageDialog(null, "You answered " + count +
" out of " + questions.length +
" questions correctly.");
}
Comment