Biased "random" number generator


I am working on a site in which users will be able to vote on, or rate, a page of content. The pages will shown randomly.

What I’m looking to do is show content with a higher rating more often. That is bias the “random” numbers to favour the display of content that is perceived by visitors to be better.

How would you guys approach this?

(pseudo code is ok)(a sql solution would be ideal)



Thanks, Stephen for that idea. It does appeal. I think I’ll do a simulation and see how it looks.

Thanks for the correction, StarLion.



You definitely should limit the RAND() to something smaller. The interval (0,1) has way too much influence over the range (0.1,9.9) to yield useful results.
The trick is to make it so small that you get mostly get higher rated result, but so small that you never see any lower results. It’s a balancing act :slight_smile:

I am.

None that I know of from the top of my head, but maybe someone else has an idea :slight_smile:

Interesting. rating is likely to be a float between 0.1 and 9.9 I may have to limit the range of RAND() so it doesn’t overwhelm the rating.

Are you then suggesting I just take the first from the resut set as the new random article?

It may work with some tweaking.


Are there any other approaches?



squaring a 0…1 number lowers it, not raises it. (0.5 ^2 = 0.25). Invert the square (sqrt or 1-square, whichever’s quicker) and you’ll produce a high-biased set you were expecting.

What about ordering them based on rating and then using a random number to decide how far into the results to go to grab the one to be used.

You could perhaps take the square of a random number between zero and one (so as to produce a bias toward higher numbers) and divide it into the number of records you want to choose between. You could then subtract the resulting number from the number of records to tell you which record in the results as sorted by rating to use.

That would avoid the extremely inefficient order by rand.

In theory ORDER BY rating*RAND() DESC should do what you ask. :slight_smile:

Dependent on the range of rating by the way, if it’s just an interval (1,5) it won’t work as good (or possibly even bad) as the interval (0,+INF)