function weightedRandom(weight, outcomes){ var total = sum( weight ); var roll = Math.random()*total; // value in the range [0,total) var seen = 0; for(let i=0; i<weight.length; i++) { seen += weight[i]; if(roll<seen) return outcomes[i]; } }