Results 1 to 4 of 4

Thread: Sqlite Random Sort Without Duplicates

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2009
    Location
    Uk
    Posts
    157

    Question 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?

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    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

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    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
  •  



Click Here to Expand Forum to Full Width