4

所以我有一个加权项目列表,我想从这个列表中挑选 4 个不重复的项目。

Item     Weight
Apple     5
Banana    7
Cherry    12
...
Orange    8
Pineapple 50

最有效的方法是什么?我最初的尝试是,如果出现了已经选择的项目,则只为随后的选择重新滚动……但是对于一个小列表,这可能会导致大量的重新滚动。

编辑澄清:对于上面的例子,忽略水果 D 到 N,总重量为 82。所以首先被采摘的机会是:A ~6% B ~8.5% C ~14.6% O ~9.8% P ~61% 一旦选择了一个项目,概率会(应该!)改变。

4

4 回答 4

6

在您的评论中,您说独特意味着:

我不想两次选择相同的项目。

.. 权重决定了被选中的可能性。

您需要做的就是确保您不选择重复项,只需在选择下一个之前从列表中删除最后一个选择的项目。是的,这会稍微改变你的权重,但如果你确实想要独特的结果,这是正确的统计改变。


此外,我不确定你是如何使用权重来确定候选者的,但我想出了这个算法,它应该用最少的循环来做到这一点(并且不需要根据权重填充数组,这可能导致非常大的数组,需要 int 权重等)

我在这里使用了 JavaScript,只是为了在没有服务器的情况下很容易在浏览器中查看输出。移植到 PHP 应该是微不足道的,因为它没有做任何复杂的事情。

常数

var FRUITS = [
    {name : "Apple", weight: 8 },
    {name : "Orange", weight: 4 },
    {name : "Banana", weight: 4 },
    {name : "Nectarine", weight: 3 },
    {name : "Kiwi", weight: 1 }
];

var PICKS = 3;

function getNewFruitsAvailable(fruits, removeFruit) {
    var newFruits = [];
    for (var idx in fruits) {
        if (fruits[idx].name != removeFruit) {
            newFruits.push(fruits[idx]);
        }
    }
    return newFruits;
}

脚本

var results = [];
var candidateFruits = FRUITS;

for (var i=0; i < PICKS; i++) {
    // CALCULATE TOTAL WEIGHT OF AVAILABLE FRUITS
    var totalweight = 0;
    for (var idx in candidateFruits) {
        totalweight += candidateFruits[idx].weight;
    }
    console.log("Total weight: " + totalweight);

    var rand = Math.random();

    console.log("Random: " + rand);

    // ITERATE THROUGH FRUITS AND PICK THE ONE THAT MATCHES THE RANDOM
    var weightinc = 0;
    for (idx in candidateFruits) {
        // INCREMENT THE WEIGHT BY THE NEXT FRUIT'S WEIGHT
        var candidate = candidateFruits[idx];
        weightinc += candidate.weight;

        // IF rand IS BETWEEN LAST WEIGHT AND NEXT WEIGHT, PICK THIS FRUIT
        if (rand < weightinc/totalweight) {
            results.push(candidate.name);
            console.log("Pick: " + candidate.name);

            // GET NEXT SET OF FRUITS (REMOVING PICKED FRUIT)
            candidateFruits = getNewFruitsAvailable(candidateFruits, candidate.name);
            break;
        }
    }
    console.log("CandidateFruits: " + candidateFruits.length);
};

输出

for (var i=0; i < results.length; i++) {
    document.write(results[i] + "<br/>");
}

基本策略是为每个水果分配总范围的一部分[0,1)。在第一个循环中,您将拥有以下内容:

  • 苹果— 8/20 = 0.0 到 0.4
  • 橙色— 4/20 = 0.4 到 0.6
  • 香蕉— 4/20 = 0.6 到 0.8
  • 油桃— 3/20 = 0.8 至 0.95
  • 猕猴桃— 8/20 = 0.95 至 1.0

该脚本遍历列表中的每个项目,并推进一个重量计数器。当它到达包含第一个随机数的范围时,它会选择该项目,将其从列表中删除,然后根据新的总重量重新计算范围并再次运行。

于 2011-06-23T18:42:50.267 回答
1

更新

function array_rand2($ary,$n = 1)
{
  // make sure we don't get in to an infinite loop
  // check we have enough options to select from
  $unique = count(array_unique(array_keys($ary)));
  if ($n > $unique) $n = count($unique);

  // First, explode the array and expand out all the weights
  // this means something with a weight of 5 will appear in
  // in the array 5 times
  $_ary = array();
  foreach ($ary as $item => $weight)
  {
    $_ary = array_merge($_ary, array_fill(0, $weight, $item));
  }

  // now look for $n unique entries
  $matches = array();
  while (count($matches) < $n)
  {
    $r = $_ary[array_rand($_ary)];
    if (!in_array($r,$matches))
    {
      $matches[] = $r;
    }
  }

  // and now grab those $n entries and return them
  $result = array();
  foreach ($matches as $match){
    $result[] = $match;
  }
  return $result;
}

看看这是否做得更好。

于 2011-06-23T18:30:59.460 回答
1

在这里,我找到了以下步骤的想法:

  1. 建立权重之和 --> SUM
  2. 在 0 和 SUM 之间构建一个随机数 --> RAND_NUMBER
  3. 遍历列表并从 RAND_NUMBER 中减去每个元素的权重。如果 RAND_NUMBER 为负数,则您有第一个元素。
  4. 从列表中删除找到的元素并返回步骤 1,直到您有 4 个元素。
于 2011-06-23T18:52:15.637 回答
0

也许您可以增加随机生成的列表元素索引而不是“重新滚动”:(list.elementAt(rand_index++ % size(list))类似的东西)。我想你会用这样的逻辑很快找到下一个随机独特的项目。

我确信有更好的解决方案,当然,通常有。

编辑:看起来布拉德已经提供了一个.. :)

于 2011-06-23T18:31:16.603 回答