1
$\begingroup$

This is technically a programming-related question, but the actual answer will most likely be a mathematical formula, and the question can be understood with no programming knowledge, so I decided not to put it in stackoverflow.

I'm making an advertising feature on a website, and the idea is if you pay more, the advertisement is more likely to show upon each page load.

For example, if Bob payed 1 thousand, and Jim paid 500, Bob's advert is twice as likely to be shown.

Given this information how could I work out which advert to show, with any number of adverts running at once? (The selection is random, in the scenario above, there's a 2/3 chance of Bob's and 1/3 change of Jim's showing. That's rather easy but imagine we had 1,000 adverts running)

Note that one advertisement may be running at once.

1 Answers 1

0

If there are $n$ customers numbered $1$ to $n$, and customer number $i$ pays $x_i$, you want to choose customer $i$ with probability $p_i = x_i/t$ where $t = \sum_{i=1}^n x_i$. To actually do the selection, take a pseudo-random number $X \in [0,1]$, and choose the first $j$ such that $\sum_{i=1}^j p_i > X$.

  • 0
    Thanks for the prompt response2011-12-18