Selecting random, weighted records from MySQL | kahunaburger

]]>Cheers – Tobias

]]>ORDER BY -LOG(1.0 – RAND()) / likelihood

For safety use likelihood > 0.0 in WHERE clause.

See http://use.perl.org/~bart/journal/33630 for excellent analysis by someone way smarter than me.

It works on all distributions I’ve tested.

]]>If someone is paying for ad time, they are going to be peeved if the frequencies don’t come out right.

I’ve seen complex and statistically correct solutions involving summing all the weights and picking a random value in between, but I’m hoping there’s a way to fix the weighting formula (e.g. rand() / likelihood ASC – gives better results).

]]>