基于概率和百分比的返回值

问题描述

任何想法,关于这些示例,我如何实现概率。

let directions = [
    {
        1: {
            left: {
                successRate: 1,direction: 0
            },top: {
                successRate: 35,direction: 1
            },right: {
                successRate: 15,direction: 2
            },bottom: {
                successRate: 5,direction: 3
            }
        },}
]
  1. 应该遍历属性
  2. 从上,右,下,左循环浏览以获取其successRate值
  3. 根据与所有内容相比的成功率(也就是概率)随机检索方向。可能性更高的人应该有更高的被叫机会。

该程序在setInterval内,并且每次间隔循环时,都假定要打印这些值。我尝试了许多不同的方法,但我无法弄清楚。

更新有关我当前遇到的实际问题。

您可以忽略“未找到”。这只是一个随机字符串。这些数字是根据概率随机生成的,但是在setInterval函数中经过x次调用之后,该数字变为“未定义”。

代码

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

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);
}

输出

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>

循环:

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

解决方法

如果我正确理解,则希望基于successRate属性上设置的概率在每次迭代中选择随机方向。如果是这样,这是我可以考虑的一种方法:

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);