Reputation: 1231
I'm creating a simple multiple choice trivia game in android. I'm going to have three choices, A, B, or C. I've created a SQLite database that contain the three possible answers to the trivia question. I've already getting it to display the correct answer. How can I write the SQL so that it selects two random unique answers from a specified range? This code:
// ---Grabs RANDOM Event ---
public String getRandomEvent() {
Cursor cursor = this.db.query("thetable Order BY RANDOM() LIMIT 1",
new String[] { KEY_EVENT }, null, null, null, null, null);
String result = "";
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
result = result
+ cursor.getString(0);
}
return result;
}
// --- END Grabs Event ---
grabs a random answer from the whole table. Also, with this code, specifically thetable Order BY RANDOM() LIMIT 1
it's possible for it to display the same answer twice. What I'd like to do is have it grab two unique answers from 10 rows above the correct answer and 10 rows below the correct answer. So basically I'd like the cursor to go to the correct answer cursor.moveToFirst();
and then select the two random answers from the 20 rows above and below the correct answer. Thanks for your help!
Upvotes: 1
Views: 180
Reputation: 180060
With the random order, "above" and "below" isn't really meaningful.
Just grab three random answers by using LIMIT 3
.
Upvotes: 1