jeudi 14 janvier 2016

Mysql Biased/Weighted Random Selection

I'm using the following query to select a random campaign:

SELECT * from (  
SELECT campaign_id
FROM campaigns
WHERE campaign_approved = 1
) T ORDER BY RAND() 
LIMIT 1

However, I'd like to select campaigns, still randomly, but with odds of being selected in proportion to their campaign_balance.

So for example, if one campaign has a campaign_balance of 50 and the other has a campaign_balance of 1.....the first one should have 50 / 51 chance of being selected.

How do I do a random selection that is biased like this?




Aucun commentaire:

Enregistrer un commentaire