0

Im very sorry if the title is a little bit misleading!

So basically i have this:

Example 1:

var array = [30,30,30]

or Example 2:

var array = [10,50,20]

I need to pick a winner from these numbers with percentages. Example: 50 wins 5 times more often than 10.

I have solution for that but it doesnt really work and is inaccurate. Update: This solution doesnt have anything to do with what i really want.

var array = [10,50,20];
var totalValue = 80;

array.sort();
array.reverse();

var lowestNumber = array.slice(-1)[0];
total += lowestNumber;

var random =  Math.floor((Math.random() * total) + lowestNumber );

  array.some(function(entry) {
 if (entry < random) {
  console.log('winner', entry)
  return true;
 }
})

This script is working on the second example, but 20 has a way to higher chance to win and the first example of course doesnt work at all.

Im happy for every answer / comment!

  • 1
    Do you want the winning chance ratio to be the same as the value in the array? Eg 50 wins 5 times as often as 10? Or, what do you want? – CertainPerformance Jun 07 '18 at 00:22
  • yes thats exactly what i need, ill update the question. – Alejandro Castro Jun 07 '18 at 00:25
  • I'm not sure the duplicate answer has a great solution to this problem.. I would do things different to that,.. First normalize your distribution from 0 to 1 incremented,. eg. `10,50,20` would become -> `0.125, 0.75, 1`, now just do a normal random, and then reverse step into the array until this random number is greater than the array value... – Keith Jun 07 '18 at 00:46
  • Thats a step forwards! but what 30,30,30 would become and how? – Alejandro Castro Jun 07 '18 at 00:58

0 Answers0