Hi, I'm after a) - the equivalent of a 'SORT BY RANDOM LIMIT x' sql statement.
> But as this isn't deterministic, I'm pretty sure it's wrong. > I don't follow your logic. The view will show all documents in a random order. The fact that is is unrepeatable may make it useless for > your purposes, but it does not make the maths invalid, or the statistics wrong. As far as I know, the couchdb internals rely on the fact that view keys are deterministic to do their view updates. I'm not entirely convinced that my current function produces a good random selection - if a document is updated more, and therefore it's view entry is updated more, does that mean it has a different chance of being selected? Cheers, Peter On 21 September 2010 20:25, Ian Hobson <[email protected]> wrote: > On 21/09/2010 18:27, Peter Braden wrote: > >> Hi, >> >> Is there a good way to get a random document from a database. >> > Hmm, that depends upon what you mean by "good", and "random" and if you > want a repeatable result! I guess I'm asking what exactly are you trying to > do? > > a) Pick a representative, and statistically defensible sample of size X > from a population of Y documents where each document has an equal > probability of being selected, and cannot be selected twice. > > b) Take a sample of size 1 from a population of Y, X times (so a given > document could be taken more than once)? > > c) Something similar to a or b where you don't know Y in advance? > > d) Shuffle the documents? > > > I'm currently > >> using a view that does: >> >> function(doc) { >> emit(Math.random(), doc); >> }; >> >> But as this isn't deterministic, I'm pretty sure it's wrong. >> > I don't follow your logic. The view will show all documents in a random > order. The fact that is is unrepeatable may make it useless for your > purposes, but it does not make the maths invalid, or the statistics wrong. > > Regards > > Ian > -- -- Peter Braden <http://PeterBraden.co.uk/>
