Random problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • saytri
    New Member
    • Dec 2007
    • 35

    Random problem

    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:

    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.");
    }
  • Stwange
    Recognized Expert New Member
    • Aug 2007
    • 126

    #2
    Maybe you should consider using an ArrayList here instead. The logic seems a little flawed and long-winded at times, but most of it looks like it could work. One error (I'm not saying it is the only one) I have identified is this loop:
    Code:
    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--;
    }
    What you are trying to do here is select an element from the array A, place it into the array B, and then remove that element from A. Technically, this usually works, but it leaves you with a bug:
    You are leaving the items at the end of the array A, and just decrementing countA so that they are not referred to any more, but in the random number generator you are using A.length, not countA, so those elements can still be selected. Try replacing A.length with countA, ie:
    Code:
    int pos = generator.nextInt(countA);
    and see if that fixes it for you.

    Comment

    • Stwange
      Recognized Expert New Member
      • Aug 2007
      • 126

      #3
      Also maybe your loop should be:
      Code:
      while (countA >= 0) {
      because A is zero-indexed (ie. A[0] is a valid element)

      Comment

      • saytri
        New Member
        • Dec 2007
        • 35

        #4
        Yeah it worked. Thanks a lot. :-)

        Comment

        Working...