I have an array of objects, and I want to randomly select one. These objects all have a performance property that ranges between [0, 1]. If this performance value is greater than (or equal to) some threshold, that element needs to be selected 40% more often than elements with a performance smaller than the threshold.
Example
var articles = [
{title: 'Article A', perf: 0.1},
{title: 'Article B', perf: 0.2},
{title: 'Article C', perf: 0.3},
{title: 'Article D', perf: 0.4},
{title: 'Article E', perf: 0.5},
{title: 'Article F', perf: 0.6}
];
The above sample list has 6 elements. If the performance threshold is, say, 0.61, then each article should have a 1/6 likelihood of being selected. However, if the threshold is now, say, 0.4, then the last three elements need to have a likelihood of being selected that is 40% greater than that of the first three elements.
What I have tried
I'm not sure if this first step is even correct, but what I'm doing is:
- Look at each element in the list:
- If the performance < threshold, I set a weight property on that element of
1 / articles.length. - If performance >= threshold, I set a weight property on that element of
(1 / articles.length) * 0.4
- If the performance < threshold, I set a weight property on that element of
The problem with that is that I then end up with a distribution that doesn't add up to 1. If I normalize it with weight = (currWeight - minWeight) / (maxWeight - minWeight), I end up with weights of [0, 0, 0, 1, 1, 1].
If I have understood the question correctly, you have a list of n elements {$x_i$} some of which are "special" (exceed some level you set) and the rest of which are non-special. You want to define two probabilities p and q where p is the probability that a given special element is chosen and q is the probability that a given non-special element is chosen. You also require that p = 1.4 q.
Assuming I have that right, then all you need is to count. If you have S special elements and N non-special elements then you know that $$pS + qN = 1$$
But this tells us that $$1 = 1.4 q S + qN = q(1.4S + N)$$
Thus $$q = \frac{1}{1.4S+N}\;\;\;and\;\;\;p = \frac{1.4}{1.4S+N}$$