Return value based on probability and percentage

Viewed 296

Any idea how I could implement probability in regards to these examples.

let directions = [
    {
        1: {
            left: {
                successRate: 1,
                direction: 0
            },
            top: {
                successRate: 35,
                direction: 1
            },
            right: {
                successRate: 15,
                direction: 2
            },
            bottom: {
                successRate: 5,
                direction: 3
            }
        },
    }
]
  1. Should loop through the properties.
  2. Loop through top,right,bottom, left to retrieve their successRate value
  3. Randomly retrieve a direction based on it's successRate (aka it's probability) in comparison to everything. The ones with the higher probability should have a higher chance of being called.

The program is within a setInterval and everytime the interval loops, it is suppose to print the values. I tried many different ways but I can't figure it out.

UPDATE For the actual problem that I am currently having.

You can ignore the "Not Found". It's just a random string. The numbers generate randomly according to the probability but after x amount of calls within a setInterval function, it becomes "undefined".

CODE:

let directions = [
    {
        1: {
            left: {
                successRate: 5,
                direction: 0
            },
            top: {
                successRate: 3,
                direction: 1
            },
            right: {
                successRate: 10,
                direction: 2
            },
            bottom: {
                successRate: 30,
                direction: 3
            }
        },
    }
]

var chances = [40, 30, 15, 10, 5]; 
let items = Object.values(directions[0][1]);

function chooseWeighted(items, chances) {
    var sum = chances.reduce((acc, el) => acc + el, 0);
    var acc = 0;
    chances = chances.map(el => (acc = el + acc));
    var rand = Math.random() * sum;
    return items[chances.filter(el => el <= rand).length];
}

function scanPlayerArea(){
    console.log(chooseWeighted(items, chances).direction);
}

OUTPUT:

Not found.
1
Not found.
0
Not found.
0
Not found.
2
Not found.
0
Not found.
1
Not found.
1
Not found.
0
Not found.
0
Not found.
C:\Users\Name\Desktop\Script\file.js:1351
    dir = chooseWeighted(items, chances).direction;
                                        ^

TypeError: Cannot read property 'direction' of undefined
    at scanPlayerArea (C:\Users\Name\Desktop\Script\file.js:1351:41)
    at Timeout._onTimeout (C:\Users\Name\Desktop\Script\file.js:1633:6)
    at listOnTimeout (internal/timers.js:549:17)
    at processTimers (internal/timers.js:492:7)

C:\Users\Name\Desktop\Script>

THE LOOP:

setInterval(function(){
    scanPlayerArea()
}, 50);
1 Answers

If I understood correctly you want to pickup a random direction on each iteration based on the probability set on the successRate property. If so, here is a way I can think of doing it:

let directions = [
    {
        1: {
            left: {
                successRate: 5,
                direction: 0
            },
            top: {
                successRate: 3,
                direction: 1
            },
            right: {
                successRate: 10,
                direction: 2
            },
            bottom: {
                successRate: 30,
                direction: 3
            }
        },
    }
];

const items = directions[0][1];

// Calculate the total rate 100%, just a sum of all the items
let totalRate = 0;
for (var key of Object.keys(items)) {
    totalRate += items[key].successRate;
}

// Calculate the coeficient of success rate for each item and save it in items
let lastCoef = 0;
for (var key of Object.keys(items)) {
  items[key].coef = lastCoef + items[key].successRate / totalRate;
  lastCoef = items[key].coef;
  console.log(key + ' coeficient: ' + lastCoef);
}

function chooseWeighted(items) {
  const randomNum = Math.random();
  for (var key of Object.keys(items)) {
    if(randomNum < items[key].coef) {
      return key;
    }
  }
}

function scanPlayerArea(){
  console.log(chooseWeighted(items));
}

setInterval(function(){
  scanPlayerArea();
}, 1000);

Related