Also if you already have many existing rows w/o a random float field,
you will need to add a random field to those rows.

You could use the remote datastore API [ ru.ly/g6 ] to add a random
field to each row in the table.  It basically connects you to the
production datastore from your local machine, and you can iterate over
all rows and ensure they have a float random field.

Robin



On Sep 10, 1:11 am, Richard <richar...@gmail.com> wrote:
> neat!
>
> On Sep 10, 3:32 pm, Robin B <robi...@gmail.com> wrote:
>
> > On GAE, the default ordering depends on which index is needed to
> > answer the query, and it is stable, so it is non-random.
>
> > To get random rows on GAE, I use a float field called random, which is
> > assigned a float from 0 and 1, then to pull random rows, I just
> > generate a random float and query by table.random < value.
>
> > Robin
>
> > On Sep 9, 10:12 pm, mdipierro <mdipie...@cs.depaul.edu> wrote:
>
> > > No but I believe that without orderby it may be random by default on
> > > GAE. I would check the GAE docs because I am not sure.
>
> > > On Sep 9, 10:01 pm, Iceberg <iceb...@21cn.com> wrote:
>
> > > > Thanks! Did not notice the '<random>' is supported by web2py DAL for
> > > > its all db backend. That is great. Does it work on GAE too?
>
> > > > BTW, my less efficient way is db-independent however.
>
> > > > On Sep10, 9:55am, mdipierro <mdipie...@cs.depaul.edu> wrote:
>
> > > > > I thought I answered this
>
> > > > > random_record = db().select(db.mytable.ALL,orderby='<random>,limitby=
> > > > > (0,1))[0]
>
> > > > > On Sep 9, 8:27 pm, Iceberg <iceb...@21cn.com> wrote:
>
> > > > > > On Sep9, 8:07pm, Richard <richar...@gmail.com> wrote:
>
> > > > > > > hi,
>
> > > > > > > is there an efficient way to get a random record from a table?
> > > > > > > An obvious non-efficient way would be to get all the records and 
> > > > > > > do
> > > > > > > random.choice().
>
> > > > > > > thanks,
> > > > > > > Richard
>
> > > > > > Would like to see comments on this.
>
> > > > > >   import random
> > > > > >   total_count = db(db.mytable.id>1).count() # well, this is one 
> > > > > > extra
> > > > > > db query
> > > > > >   random_id = random.randint(1, total_count)
> > > > > >   random_record = db.mytable[random_id] # Does this work on GAE?
>
> > > > > > Iceberg
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To post to this group, send email to web2py@googlegroups.com
To unsubscribe from this group, send email to 
web2py+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to