Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting random entry from Objectify entity

How can I get a random element out of a Google App Engine datastore using Objectify? Should I fetch all of an entity's keys and choose randomly from them or is there a better way?

like image 394
Ben Flynn Avatar asked Mar 22 '26 11:03

Ben Flynn


1 Answers

Assign a random number between 0 and 1 to each entity when you store it. To fetch a random record, generate another random number between 0 and 1, and query for the smallest entity with a random value greater than that.

like image 179
Nick Johnson Avatar answered Mar 24 '26 14:03

Nick Johnson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!