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
}
},
}
]
- Should loop through the properties.
- Loop through top,right,bottom, left to retrieve their successRate value
- 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);