Get random rows from DB

Get random rows from DB

by George Schaathun -
Number of replies: 0
Picture of Plugin developers

I am looking for an API function to get random rows from a DB table, i.e. implementing the equivalent of MySQL's ORDER BY RAND() (c.f. https://www.petefreitag.com/item/466.cfm for other DB engine equivalents).  The closest answer google would give me is this: https://moodle.org/mod/forum/discuss.php?d=163149 .  Evidently difficult/impossible, but then ten years has passed.  Does anyone know if a get_random_row API function has been added to Moodle since then?

Yes, I know I can do it in PHP, but I do not want to load 1000 questions to draw five.  The case at hand, if it helps to know, is CAPQuiz, where I want to select the N questions closest to a given target rating.  This works well as long as questions do not tie for the same rating.  When ties occurs, it is important to randomise to get the desired distribution of question.

Thanks for any advice

:-- George

Average of ratings: -