Hi,
Currently, there is no way to directly query a random entity, but one
of our engineers has taken the following approach:
If you want to be able to access a random entity for a certain object
model, you can do it in this way:
* When inserting the entity, have a field called "rand" (or any
other name), and store a random value between 0 and 1 when the entity
is created.
import random
class MyModel(db.Model):
rand = db.FloatProperty()
first_property = db.StringProperty()
main():
model = MyModel()
model.rand = random.random()
model.put()
* Make sure that the very first value entered has a value of 1.0
for this field. You can do this via a special case in the code, or by
manually editing it with the admin console.
* When querying, use the following GQL query: "SELECT * FROM
MyModel WHERE rand > :rand ORDER BY rand LIMIT 1", and bind a random
float to rand.
While you are not guaranteed that all entities will be equally likely
to appear, this should give a relatively good random distribution. It
also will scale, since there are no locks needed to get a uniformly
incrementing counter.
Hope this helps,
Marzia