-1

I have an array of agents which each have the attribute rate which is their accepted calls minus their declined calls. I order these agents in the array based on the rate. I'd like to give agents with a better rate a higher chance to be selected for a call than other agents with a lower rate to make things fair. I understand how to select a random element from an array but I can't wrap my head around this problem.

Jake Cross
  • 523
  • 1
  • 6
  • 14
  • 2
    please show us some code of what you have tried so far. – DigitalJedi Jun 01 '19 at 19:36
  • 1
    To clarify your question, do you mean that the agents with a higher attribute rate have a greater chance of being randomly selected? – Samuel Cooper Jun 01 '19 at 19:39
  • @SamuelCooper Correct. – Jake Cross Jun 01 '19 at 19:39
  • You can find some good ideas [here](https://stackoverflow.com/questions/8485860/unbalanced-random-number-generator) – m4gic Jun 01 '19 at 19:42
  • You'd be looking to use a distribution, such as the Pareto distribution to generate random numbers. I believe that JS uses a uniform-like distribution so it will not fit your needs. I'll have a quick look and see if there is a library that could provide you with the desired distribution. – Samuel Cooper Jun 01 '19 at 19:42
  • This is a simple weighted-average problem. Please don't overthink this. – Randy Casburn Jun 01 '19 at 19:43

1 Answers1

1

In this code, there is a 2/15 chance to get 2, 3/15 chance to get 3, and so on.
15 is based on the total rate of all the agents.

var aoa = [{rate: 2},{rate: 3},{rate: 6},{rate: 4}];
var r = Math.random() * aoa.map(e => e.rate).reduce((ac,c) => ac+c,0);
var sa;
aoa.forEach((a,i) => {
    var pt = aoa.slice(0,i).map(e => e.rate).reduce((ac,c) => ac+c,0); 
    if (r >= pt && r < pt+a.rate) sa = a;
    console.log(`${r} has to be between ${pt} and ${pt+a.rate} if chosen rate =  ${a.rate}`);
});
console.log(sa);
Yousername
  • 1,012
  • 5
  • 15