How do I make it show answers randomly on one of 4 lines, without having any duplicates?
My current code:
TextView question;
private int qType = -1;
private int asked = 0;
private void QBegin() {
/*
* Gets a random question
*/
question = (TextView) findViewById(R.id.question);
String[] types = { "Q1", "Q2", "Q3", "Q4", "Q5"};
Random random = new Random();
int qType = random.nextInt(types.length);
question.setText(types[qType]);
asked++;
// StringList.add(types[qType]);
getAnswers(qType);
/* if(StringList.contains(types[qType]) && asked >= types.length+1){
asked = 0;
answerCounter.setText("THE END");
} else if (StringList.contains(types[qType]) && asked < types.length+1){
QBegin();
} */
}
public static int random(int range) {
return (int)(java.lang.Math.random() * (range+1));
}
public void shuffle(String input){
/*
* Unused shuffle method
*/
List<Character> characters = new ArrayList<Character>();
for(char c:input.toCharArray()){
characters.add(c);
}
StringBuilder output = new StringBuilder(input.length());
while(characters.size()!=0){
int randPicker = (int)(Math.random()*characters.size());
output.append(characters.remove(randPicker));
}
System.out.println(output.toString());
}
private void getAnswers(int Type) {
/*
* Getting answers here
*/
int randomValue = random(4);
try {
String answers_list[][] = {
{"Answer 1-1", "Answer 2-1", "Answer 3-1", "Answer 4-1"},
{"Answer 1-2", "Answer 2-2", "Answer 3-2", "Answer 4-2"},
{"Answer 1-3", "Answer 2-3", "Answer 3-3", "Answer 4-3"},
{"Answer 1-4", "Answer 2-4", "Answer 3-4", "Answer 4-4"},
{"Answer 1-5", "Answer 2-5", "Answer 3-5", "Answer 4-5"}} ;
answer1.setText(answers_list[Type][randomValue+1 > 3 ? (randomValue+0)-4 : randomValue+0]);
answer2.setText(answers_list[Type][randomValue+2 > 3 ? (randomValue+1)-3 : randomValue+1]);
answer3.setText(answers_list[Type][randomValue+3 > 3 ? (randomValue+2)-2 : randomValue+2]);
answer4.setText(answers_list[Type][randomValue+0 > 3 ? (randomValue+3)-4 : randomValue+3]);
/*for (int rows = 0; rows < answer&*list.length; rows++){
for (int cols = 0; cols < answers_list[rows].length; cols++){
}
}*/
} catch(Exception ex){
answer1.setText("Error "+ex);
}
}
Code that's responsible for picking & adding questions on one of 4 lines (randomly)
answer1.setText(answers_list[Type][randomValue+1 > 3 ? (randomValue+0)-4 : randomValue+0]);
answer2.setText(answers_list[Type][randomValue+2 > 3 ? (randomValue+1)-3 : randomValue+1]);
answer3.setText(answers_list[Type][randomValue+3 > 3 ? (randomValue+2)-2 : randomValue+2]);
answer4.setText(answers_list[Type][randomValue+0 > 3 ? (randomValue+3)-4 : randomValue+3]);
At the moment, I'm having duplicates with answer1 and answer4. Please Help.
-
It's... unclear what's happening here, or how you're implementing this. Basically you're trying to get a list of four random numbers with no duplicates?Dave Newton– Dave Newton2011年10月08日 17:22:06 +00:00Commented Oct 8, 2011 at 17:22
-
Yes, that's what I'm trying to do.Alex– Alex2011年10月08日 17:41:24 +00:00Commented Oct 8, 2011 at 17:41
2 Answers 2
If I understand your goal correctly, you're trying to randomly shuffle a list of questions. Rossum gave you one way to do this and this is a good introduction to the best known options.
In my opinion, this is the simplest approach:
- Create a map that sorts by key (e.g.
TreeMap <Double,String>or<Double,Question>) - Add a random key and the question as the value
- Loop through and print the results. A foreach loop will use the sort order
Don't worry about duplicate random numbers or normalizing it. Dups are unlikely and if you need a question #, add a counter in the loop.
Comments
There is a known standard shuffling method.
- Pick one of the four at random. If necessary, swap the one you picked with the answer at position four.
- Pick one of the remaining answers 1 to 3 at random. If necessary, swap the one you picked with the answer at position three.
- Pick one of the remaining answers 1 to 2 at random. If necessary, swap the one you picked with the answer at position two.
You now have a shuffled list containing the original four answers in random order. This algorithm is called the Fisher-Yates shuffle.
ETA simple sample code:
Random rand = new Random();
String[] answers = { "42",
"Only on a Tuesday.",
"4ドル.36",
"Hieronymous K. Sluggenheimer III" };
// Shuffle answers[]
for (int i = 3; i > 0; --i) {
// Pick an answer that hasn't yet been chosen.
int pick = rand.nextInt(i + 1);
if (pick != i) {
// Exchange answers[i] and answers[pick].
String temp = answers[i];
answers[i] = answers[pick];
answers[pick] = temp;
}
}