使用加权概率和值在数组中查找项目 [英] Find item in array using weighed probability and a value

查看:91
本文介绍了使用加权概率和值在数组中查找项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上周我遇到了一些我正在做的简单程序的问题,而且有人帮我了。现在我遇到了另一个问题。
我目前有这个代码:

Last week I had some problems with a simple program I am doing and somebody here helped me. Now I have run into another problem. I currently have this code:

var findItem = function(desiredItem) {
    var items = [
        { item: "rusty nail", probability: 0.25 },
        { item: "stone", probability: 0.23 },
        { item: "banana", probability: 0.20 },
        { item: "leaf", probability: 0.17 },
        { item: "mushroom", probability: 0.10 },
        { item: "diamond", probability: 0.05 }
    ];
    var possible = items.some( ({item, probability}) => 
          item === desiredItem && probability > 0 );
    if (!possible) {
        console.log('There is no chance you\'ll ever find a ' + desiredItem);
        return;
    }
    var sum = items.reduce( (sum, {item, probability}) => sum+probability, 0 );
    while (true) {
        var value = Math.random() * sum;
        var lootedItem = items.find( 
                ({item, probability}) => (value -= probability) <= 0 ).item;
        if (lootedItem === 'diamond') break;
        console.log("Dang! A " + lootedItem + " was found...");
    }
    console.log("Lucky! A " + desiredItem + " was found!");
}

findItem('diamond');

现在我想通过在中添加另一个名为 category 的值来扩展它items 数组。我希望类别的值为 2 5 10 。因此,假设钻石项目属于类别:10 ,以及 findItem ,只能找到属于同一类别的项目。我已经尝试了几天,但似乎无法理解它。也许有人可以帮我推动正确的方向?提前致谢

Now I would like to expand on this by adding another value called category to the items array. I want the categories to have a value of either 2, 5 or 10. So let's say the diamond item would belong to category: 10, and when findItem is executed only items that belong to the same category can be found. I have been trying for a couple of days now but can't seem to get my head around it. Maybe someone can help push me in the right direction? Thanks in advance

推荐答案

您可以将此更新用于该代码:

You could use this update to that code:

// Pass the item list and the desired category as arguments:
var findItem = function(items, category, desiredItem) {
    // apply filter to items, so only those of the given category remain:
    items = items.filter( item => item.category == category );
    // rest of code remains the same:
    var possible = items.some( ({item, probability}) => 
          item === desiredItem && probability > 0 );
    if (!possible) {
        console.log('There is no chance you\'ll ever find a ' + desiredItem);
        return;
    }
    var sum = items.reduce( (sum, {item, probability}) => sum+probability, 0 );
    var t = 10;
    while (true) {
        var value = Math.random() * sum;
        var lootedItem = items.find( 
                ({item, probability}) => (value -= probability) <= 0 ).item;
        if (lootedItem === desiredItem) break; // fixed this condition!
        console.log("Dang! A " + lootedItem + " was found...");
        t--; if (t <= 0) throw "loop";
    }
    console.log("Lucky! A " + desiredItem + " was found!");
}

// Define items here with their category
var items = [
    { item: "rusty nail", probability: 0.25, category:  2 },
    { item: "stone",      probability: 0.23, category:  2 },
    { item: "banana",     probability: 0.20, category:  2 },
    { item: "leaf",       probability: 0.17, category:  5 },
    { item: "mushroom",   probability: 0.10, category:  5 },
    { item: "diamond",    probability: 0.05, category: 10 }
];

// Call function with extra arguments:
findItem(items, 5, 'mushroom');

console.log('second run:');
// This will obviously give a hit immediately, as there is only one possible item:
findItem(items, 10, 'diamond');

更改为:


  • 将更多参数传递给您的函数:项目列表和所需类别

  • 在项目列表中应用过滤器作为函数中的第一个操作

  • 修复了与 lootedIt有关的问题em test - 它有菱形硬编码。

  • 定义函数外部的项目列表,并为每个元素添加类别值。

  • 调整函数的调用以传递额外的参数。

  • Pass more arguments to your function: the items list and the desired category
  • Apply a filter on the items list as first action in the function
  • Fix an issue concerning the lootedItem test -- it had "diamond" hard-coded.
  • Define the items list outside of the function and add category values to each element.
  • Adapt the call of the function to pass the extra arguments.

这篇关于使用加权概率和值在数组中查找项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆