我有一个像
这样的数字列表 $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中内置了什么,那将是一个很大的帮助。非常感谢你。
答案 0 :(得分:3)
您正在寻找的基本算法:
示例代码:
<?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