You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Troy Kruthoff <tk...@blit.com> on 2009/02/04 20:15:38 UTC

_rev for random view???

Anyone have thoughts on the best way to create a view of random  
documents?  I'm thinking a view with emit(doc._rev,null) would be  
sufficient for my needs, but thought I'd ping the list for collective  
knowledge ;)

-- troy



Re: _rev for random view???

Posted by Jan Lehnardt <ja...@apache.org>.
On 4 Feb 2009, at 21:18, Chris Anderson wrote:

> On Wed, Feb 4, 2009 at 11:15 AM, Troy Kruthoff <tk...@blit.com>  
> wrote:
>>
>> Anyone have thoughts on the best way to create a view of random  
>> documents?
>> I'm thinking a view with emit(doc._rev,null) would be sufficient  
>> for my
>> needs, but thought I'd ping the list for collective knowledge ;)
>>
>
> I'm not sure how _rev works for randomness.


couch_db.erl:                 
Doc#doc{revs=[list_to_binary(integer_to_list(couch_util:rand32())) |  
Revs]}

Cheers
Jan
--

Re: _rev for random view???

Posted by Paul Davis <pa...@gmail.com>.
On Wed, Feb 4, 2009 at 3:47 PM, Chris Anderson <jc...@apache.org> wrote:
> On Wed, Feb 4, 2009 at 12:41 PM, Paul Davis <pa...@gmail.com> wrote:
>> On Wed, Feb 4, 2009 at 3:18 PM, Chris Anderson <jc...@apache.org> wrote:
>>> On Wed, Feb 4, 2009 at 11:15 AM, Troy Kruthoff <tk...@blit.com> wrote:
>>>>
>>>> Anyone have thoughts on the best way to create a view of random documents?
>>>>  I'm thinking a view with emit(doc._rev,null) would be sufficient for my
>>>> needs, but thought I'd ping the list for collective knowledge ;)
>>>>
>>>
>>> I'm not sure how _rev works for randomness.
>>>
>>> Math.random()
>>>
>>
>> Though you should *NEVER* use Math.random() in a view. Not sure if
>> it's even in the sandbox, but just in case.
>
> I don't think there's anything wrong with using it. If what you want
> is a random order.
>
> More efficient might be to take the seqid of the db, and generate a
> random # smaller than it. Then chose the docid based on the seq.
> however, that will bias for frequently updated docs.
>

http://xkcd.com/221/

Is the desired behavior really to have an output that is the same
'random' on every request? (minus the sameness of docs that were
updated since the last query)

> --
> Chris Anderson
> http://jchris.mfdz.com
>

Re: _rev for random view???

Posted by Troy Kruthoff <tk...@blit.com>.
Math.random() is avail, at least in 0.9.0a712819 and I hope it stays,  
most RDBMS have support for generating random numbers as well.  I  
realize this could be done @ the app level, but it's so much cleaner  
to me to have a view called random and be done with it.

-- troy



On Feb 4, 2009, at 12:47 PM, Chris Anderson wrote:

> On Wed, Feb 4, 2009 at 12:41 PM, Paul Davis <paul.joseph.davis@gmail.com 
> > wrote:
>> On Wed, Feb 4, 2009 at 3:18 PM, Chris Anderson <jc...@apache.org>  
>> wrote:
>>> On Wed, Feb 4, 2009 at 11:15 AM, Troy Kruthoff  
>>> <tk...@blit.com> wrote:
>>>>
>>>> Anyone have thoughts on the best way to create a view of random  
>>>> documents?
>>>> I'm thinking a view with emit(doc._rev,null) would be sufficient  
>>>> for my
>>>> needs, but thought I'd ping the list for collective knowledge ;)
>>>>
>>>
>>> I'm not sure how _rev works for randomness.
>>>
>>> Math.random()
>>>
>>
>> Though you should *NEVER* use Math.random() in a view. Not sure if
>> it's even in the sandbox, but just in case.
>
> I don't think there's anything wrong with using it. If what you want
> is a random order.
>
> More efficient might be to take the seqid of the db, and generate a
> random # smaller than it. Then chose the docid based on the seq.
> however, that will bias for frequently updated docs.
>
> -- 
> Chris Anderson
> http://jchris.mfdz.com


Re: _rev for random view???

Posted by Chris Anderson <jc...@apache.org>.
On Wed, Feb 4, 2009 at 12:41 PM, Paul Davis <pa...@gmail.com> wrote:
> On Wed, Feb 4, 2009 at 3:18 PM, Chris Anderson <jc...@apache.org> wrote:
>> On Wed, Feb 4, 2009 at 11:15 AM, Troy Kruthoff <tk...@blit.com> wrote:
>>>
>>> Anyone have thoughts on the best way to create a view of random documents?
>>>  I'm thinking a view with emit(doc._rev,null) would be sufficient for my
>>> needs, but thought I'd ping the list for collective knowledge ;)
>>>
>>
>> I'm not sure how _rev works for randomness.
>>
>> Math.random()
>>
>
> Though you should *NEVER* use Math.random() in a view. Not sure if
> it's even in the sandbox, but just in case.

I don't think there's anything wrong with using it. If what you want
is a random order.

More efficient might be to take the seqid of the db, and generate a
random # smaller than it. Then chose the docid based on the seq.
however, that will bias for frequently updated docs.

-- 
Chris Anderson
http://jchris.mfdz.com

Re: _rev for random view???

Posted by Paul Davis <pa...@gmail.com>.
On Wed, Feb 4, 2009 at 3:18 PM, Chris Anderson <jc...@apache.org> wrote:
> On Wed, Feb 4, 2009 at 11:15 AM, Troy Kruthoff <tk...@blit.com> wrote:
>>
>> Anyone have thoughts on the best way to create a view of random documents?
>>  I'm thinking a view with emit(doc._rev,null) would be sufficient for my
>> needs, but thought I'd ping the list for collective knowledge ;)
>>
>
> I'm not sure how _rev works for randomness.
>
> Math.random()
>

Though you should *NEVER* use Math.random() in a view. Not sure if
it's even in the sandbox, but just in case.

> works fine.
>
>
> --
> Chris Anderson
> http://jchris.mfdz.com
>

Re: _rev for random view???

Posted by Chris Anderson <jc...@apache.org>.
On Wed, Feb 4, 2009 at 11:15 AM, Troy Kruthoff <tk...@blit.com> wrote:
>
> Anyone have thoughts on the best way to create a view of random documents?
>  I'm thinking a view with emit(doc._rev,null) would be sufficient for my
> needs, but thought I'd ping the list for collective knowledge ;)
>

I'm not sure how _rev works for randomness.

Math.random()

works fine.


-- 
Chris Anderson
http://jchris.mfdz.com

Re: _rev for random view???

Posted by Paul Davis <pa...@gmail.com>.
On Wed, Feb 4, 2009 at 2:15 PM, Troy Kruthoff <tk...@blit.com> wrote:
>
> Anyone have thoughts on the best way to create a view of random documents?
>  I'm thinking a view with emit(doc._rev,null) would be sufficient for my
> needs, but thought I'd ping the list for collective knowledge ;)
>
> -- troy
>
>
>

You might try using a _list and then emit views randomly. Though
you'll want to check out the math on how to guarantee that you're not
biasing towards the beginning of your list.

HTH,
Paul Davis