Artificial intelligent assistant

Randomly selecting an object depending on the number associated with it. 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.

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$.

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy 65e8fe2d011a8cf1bca9d5dee7c13f0e