Hello,

please see this thread 
http://couchdb.markmail.org/search/selecting+a+random+subset+of+a+view for good 
ideas to implement randomness. I opened a Jira ticket to have some random API 
option, but for now it's still only a wish.

Mickael

----- Mail Original -----
De: "Aaron Miller" <[email protected]>
À: [email protected]
Envoyé: Jeudi 23 Septembre 2010 09h10:43 GMT +01:00 Amsterdam / Berlin / Berne 
/ Rome / Stockholm / Vienne
Objet: Re: Random Document

Use a hash, like sha1 to transform the non-uniform id distribution to a
random distribution deterministially. emit(sha1(doc._id)). (plenty of JS
SHA1 hash libs around)
Then to pull a random value out, query the view with startkey=sha1(random
number gen'd at query time) and limit=1.

On Tue, Sep 21, 2010 at 10:27 AM, Peter Braden <
[email protected]> wrote:

> Hi,
>
> Is there a good way to get a random document from a database. 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've done a bit of googling, and haven't found anything.
>
> Cheers,
>
> Peter
>
>
>
> --
> Peter Braden
>
> <http://PeterBraden.co.uk/>
>

Reply via email to