PDA

View Full Version : ORDER BY RAND() LIMIT 1 is not random enough...


SYP}{ER
12-28-2002, 03:55 PM
I'm using a query based on that, but the results aren't nearly random enough! I keep getting the blond joke over and over and only sometimes getting different once here and there. What's the problem?

http://rbnb.aaron-wright.com/rand2.php?from_section%5B%5D=quotes&from_section%5B%5D=jokes&maxlength=9000&style=onelinerhtml

The query is shown on the page beside the //.

Thanks for any help :)

Oh, and if it matters, the blonde joke that keeps popping up happens to be the very first blond joke entered...

Spookster
12-28-2002, 06:12 PM
How many records are there total? Randomness out of 20 is not going to be as random as out of 2000. Also you are putting constraints on the records you are selecting for the random pick. That will decrease the number of records to be chosen from thus decreasing the randomness.

SYP}{ER
12-28-2002, 06:48 PM
There are 238 entries, but even still, I would think that with about 20 jokes to choose from, the same one nearly every time isn't right :S

And those contraints are hardly contraints... They just tell which "section" to pull from (either jokes or facts).

How should I rewrite my query then?

Spookster
12-28-2002, 09:00 PM
What is this constraint for:

LENGTH(body)<9000

With randomness and computers picking a random number between 1 and 20 is won't seem very random and getting the same number often is quite common.

You would stand a better chance of getting random results by generating a random number with PHP and then select the corresponding record. Using system time is a nice way of generating random numbers.

Spookster
12-28-2002, 10:46 PM
Here are some examples:

http://www.microcyb.com/?m=c&c=79

http://www.grmi.org/~www/phpexamples/demo_rand.phtml

http://www.ftls.org/en/examples/php/random_txt.php3

http://www.czaries.net/scripts/

SYP}{ER
12-29-2002, 03:24 AM
I got it now :) Just needed time to relax and think of an action plan ;)

One of those links inspired me (think it was the last one) and I managed to do the same thing they do in a more flexible way with way less lines :D

Thanks all.

bryndyment
08-08-2003, 01:33 AM
I just ran into this same problem. If you use MySQL 3.23.56 or later, you'll get much "healthier" random results when using ORDER BY RAND().