-2

I have this method that generates random questions, I want to be able to generate each question once and no more than once. How could I do that?

This is the code so far:

package boss;
import java.util.Random;
import javax.swing.JFrame;


public class Boss {
    public static void main(String[] args) {

        LoginWindow window = new LoginWindow();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setVisible(true);
    }

    public String getQuestions() {
        String [] question = new String[30];
        question[0] = "hello";
        question[1] ="yo";
        question[2] ="b";
        question[3] ="ha";

        //Generating random questions
        Random r = new Random();
        int i=r.nextInt(4);
        String quest=question[i];
        return quest;
    }
}
martin clayton
  • 76,436
  • 32
  • 213
  • 198
Walid
  • 73
  • 1
  • 1
  • 9

7 Answers7

5

You're not generating questions in your example - you're picking them from a fixed set stored in an array. It sounds like you just want to shuffle the array, then iterate over part of it until you have seen the required number of questions. So - suggest you shuffle the questions, then just iterate over the shuffled array, or shuffle an array of indexes 0..n and iterate over those in the original list of questions.

There's plenty of approaches to shuffling, perhaps the simplest is a single pass over the input data, swapping each element with some other randomly chosen element.

martin clayton
  • 76,436
  • 32
  • 213
  • 198
4

Use an ArrayList instead of a table. Removes the displayed question from the ArrayList when it has been displayed.

Baptiste Gousset
  • 251
  • 2
  • 16
1

A fairly simple solution would be to keep a record of all the questions you have asked and only generate ones that you have not:

private ArrayList<Integer> questionsAsked = new ArrayList<>();

public String getQuestions()
{
    String [] question = new String[30];
    question[0] = "hello";
    question[1] ="yo";
    question[2] ="b";
    question[3] ="ha";

    //Generating random questions
    Random r = new Random();
    int i = r.nextInt(question.length);

    //keep looping until you find a question you have not asked     
    while(questionsAsked.contains(i))
    {
       i = r.nextInt(question.length);
    }

    //add that question to the list of questions already asked
    questionsAsked.add(i);

    //ask the question
    return question[i];
}
Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
1

you have to keep a list of the ones you've already used and check against that.

boolean used[] = new boolean[30];
int i;

do {
    Random r = new Random();
    i=r.nextInt(4);

} while(used[i] == true);

String quest=question[i];
used[i] = true;
twain249
  • 5,666
  • 1
  • 21
  • 26
1

You can use Collections.shuffle together with queue remove and lazy question generation, working example:

import java.util.*;

public class Mkt {
  private Queue<String> questions = null;

  public Mkt() {
    for(int i = 0; i < 10; i++) {
      System.out.println(getQuestion());
    }
  }

  public String getQuestion() {
    if(questions == null || questions.size() == 0) {
      questions = generateQuestions();
    }
    return questions.remove();
  }

  private Queue<String> generateQuestions() {
    List<String> list = Arrays.asList("hello", "yo", "b", "ha");
    Collections.shuffle(list);
    return new LinkedList<String>(list);
  }

  public static void main(String[] args) {
    new Mkt();
  }
}

Sample run:

$ javac Mkt.java && java Mkt
ha
yo
hello
b
b
ha
hello
yo
hello
ha
icyrock.com
  • 27,952
  • 4
  • 66
  • 85
1

You could solve this with a "shuffle" algorithm. Basically randomize (shuffle) your array and then just pick the next item from the list.

One of the easiest shuffle algorithms is Knuth's: http://en.wikipedia.org/wiki/Knuth_shuffle

Pseudocode to shuffle your array:

Random rand = new Random();
    for (int i=questions.Length-1; i>=0; --i)
    {
        int nextRand = rand.Next(i);

        // Switch the randomly selected 'next' to the current pointer in the array
        string temp = questions[nextRand];
        questions[nextRand] = i;
        questions[i] = temp;
    }
krolth
  • 1,032
  • 9
  • 12
0

Keep track of what you've already picked.

String [] question = new String[30];
boolean[] picked = new boolean[30];
...
if (!picked[i])
{
    String quest=question[i];
    picked[i] = true;
}
else
    // choose another

(Obviously you'll need to restructure your code and also deal with knowing when you've exhaused your question supply and all have been picked)

Brian Roach
  • 76,169
  • 12
  • 136
  • 161