-
Feb 20th, 2015, 01:07 PM
#1
Thread Starter
Addicted Member
Sqlite Random Sort Without Duplicates
I have an exam simulator which loads questions from an sqlite database
If i use Order By RANDOM() it randomly sorts the questions which is great, however it duplicates some of the questions
Code:
"Select * from exam Order By RANDOM() LIMIT 1 OFFSET"
How can i randomly pick the questions without picking the same questions more than once?
-
Feb 20th, 2015, 01:14 PM
#2
Re: Sqlite Random Sort Without Duplicates
You may not be able to through SQL, but you can always do that in code, so if somebody doesn't show a way to avoid duplicates in the query, just load them all into some collection in code and work with that.
My usual boring signature: Nothing
-
Feb 20th, 2015, 01:45 PM
#3
Re: Sqlite Random Sort Without Duplicates
If you're getting duplicate questions, then they are duplicated in the table. There is nothing inherent in that query that would produce duplicate records. Besides, you're limiting it to a single question, so I'm not sure how there are duplicates.
But... if you meant that, you run it once, you get question #34, run it again, get #89 then run it again and get #34 again... sure, that could happen... If you now you want 3 questions select 3 questions all at once... if you need 10, select 10 all at once... Don't keep going back to the database and ask for jsut one more random question, because true randoms will produce duplicates. Probability says it to be so. Especially if you leave it in the stack of possibles...
Change your limit to return the correct number of questiosn you want returned.
-tg
-
Feb 20th, 2015, 01:47 PM
#4
Re: Sqlite Random Sort Without Duplicates
Use the distinct keyword to get non-duplicate data. So..
Select Distinct * From Exam Order By Random()
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|