2

我有一个数字列表,例如

 $list = array(1,5,19,23,59,51,24) 

在实际代码中,这是从数据库生成的,因此该数组最多可容纳 500 个彼此不同的数字。

数据库中的这些数字中的每一个都有记录的发生概率。所以我有以前执行的数据来生成从 1 到 500 的随机数,并记录了每个数字生成 1000 次的概率。

现在有了每个数字的数字和概率列表,我想编写一个函数,该函数将根据它们的概率从这 500 个数字中生成一个随机数。

例如:

    number 1 has a chance of: 0.00123 //0.123%
    number 6 has a chance of: 0.0421 //4.21%
    number 11 has a chance of: 0.0133 //1.33%

所以变量 $finallist 看起来像这样:

   $finallist[1] = 0.00123;
   $finallist[6] = 0.0421;
   $finallist[11] = 0.0133;

现在,如果我运行我的函数并传入 $finallist 作为参数,我想检索一个介于 1 和 6 之间的随机数,但数字 6 出现的可能性高于 1,而 11 出现的可能性高于 1。

我编写了一些函数来处理根据机会返回随机数,但它只需要 1 个值作为参数。

private function randomWithProbability($chance, $num, $range = false)
{
    /* first generate a number 0 and 1 and see if that number is in the range of chance */
    $rand = $this->getRandomFloatValue(0, 1);

    if ($rand <= $chance) 
    {
        /* the number should be returned */
        return $num;
    }
    else 
    {
        /* otherwise return a random number */
        if ($range !== false)
        {
            /* make sure that this number is not same as the number for which we specified the chance */
            $rand = mt_rand(1, $range);
            while ($rand == $num)
            {
                $rand = mt_rand(1, $range);
            }

            return $rand;
        }
    }
}

如果有人知道执行此操作的解决方案/算法,或者 PHP 中是否有任何内置内容,那将是一个很大的帮助。非常感谢。

4

1 回答 1

3

您正在寻找的基本算法:

  • 将所有概率相加并确定最大值
  • 选择一个 0 到 1 之间的随机数并将其乘以最大值
  • 找到与该值对应的条目

示例代码:

<?php

// create some weighted sample data (id => weight)
$samples = array(
  'a' => 0.001,
  'b' => 0.004,
  'c' => 0.006,
  'd' => 0.05,
  'e' => 0.01,
  'f' => 0.015,
  'g' => 0.1
);

class Accumulator {
   function __construct($samples) {
      // accumulate all samples into a cumulative amount (a running total)
      $this->acc = array();
      $this->ids = array();
      $this->max = 0;
      foreach($samples as $k=>$v) {
         $this->max += $v;
         array_push($this->acc, $this->max);
         array_push($this->ids, $k);
      }
   }

   function pick() {
      // selects a random number between 0 and 1, increasing the multiple here increases the granularity
      // and randomness; it should probably at least match the precision of the sample data (in this case 3 decimal digits)
      $random = mt_rand(0,1000)/1000 * $this->max;
      for($i=0; $i < count($this->acc); $i++) {
         // looks through the values until we find our random number, this is our seletion
         if( $this->acc[$i] >= $random ) {
            return $this->ids[$i];
         }
      }
      throw new Exception('this is mathematically impossible?');
   }

   private $max; // the highest accumulated number
   private $acc; // the accumulated totals for random selection
   private $ids; // a list of the associated ids
}

$acc = new Accumulator($samples);

// create a results object to test our random generator
$results = array_fill_keys(array_keys($samples), 0);

// now select some data and test the results
print "picking 10000 random numbers...\n";
for($i=0; $i < 10000; $i++) {
   $results[ $acc->pick() ]++;
}

// now show what we found out
foreach($results as $k=>$v) {
   print "$k picked $v times\n";
}

结果:

> php.exe rand.php
picking 10000 random numbers...
a picked 52 times
b picked 198 times
c picked 378 times
d picked 2655 times
e picked 543 times
f picked 761 times
g picked 5413 times

使用此示例运行相同的代码:

// samples with even weight
$samples = array(
   'a' => 0.1,
   'b' => 0.1,
   'c' => 0.1,
   'd' => 0.1
);

产生这些结果:

> php.exe rand.php
picking 10000 random numbers...
a picked 2520 times
b picked 2585 times
c picked 2511 times
d picked 2384 times
于 2012-10-28T01:50:10.473 回答