Sometimes it amazes me how easy it is to do some things using App Engine/Django, and how some simple things are a goddamn science. This one is somewhere in the middle. Getting a random entity from the Datastore. This is how you do it!
See what happens here? We assign a random float to the entity on it's creation - they will look something like this in the datastore:
0.216565955485Then, when doing a query, we simply create a new random value and query the Datastore for a single entity that is larger than that random value. In the off chance that this query returns no results, we'll have a backup query that runs the whole thing in reverse. This can happen if you have very few entities, and none of them have a random value in the upper ranges:
0.6289122919910.416565323566
0.216565955485
0.118278328322
0.013212121212
If the random value you send into the GqlQuery above is 0.898912291991, it will return no rows. Therefore, the backup query is needed. The system will, of course, rely less and less on the backup query as more entities are added.
3 kommentarer:
My "random page" implementation is much simpler, i think.
Given a model MyModel that has a strictly increasing integer property "index", and a counter that contains the current high index:
index = random.randint(2,counter.count + 1)
model = MyModel.all().filter('index <', index).order('-index').get()
Both your implementations do not give an even distribution over the items in the datastore...
Not sure what the best solution would be, but the suggested backup query? I'd just select the smallest item, closest to 0, rather than reversing the sorting order and selecting the largest... thereby emulating a "wrap-around" float.
Still doesn't provide an even distribution though, but doesn't have the bias that favours the largest and "disfavours" the smallest. Not that that bias is significant, the variance in distribution would have a much larger effect. (Likelihood of an item being selected depends on its distance from the smaller item.)
Wow, U might be genius to learn all that codes! I am dizzy here! ^^
Skicka en kommentar