PHP选择带权重的随机数

时间:2012-05-11 21:53:00

标签: php random probability

假设我想随机choose 1-10来自1 - 15% chance 2 - 15% chance 3 - 12% chance 4 - 12% chance 5 - 10% chance 6 - 10% chance 7 - 8% chance 8 - 8% chance 9 - 5% chance 10 - 5% chance ,但每个数字都有权重。

PHP

我将如何在{{1}}中对此进行编码?

4 个答案:

答案 0 :(得分:4)

我认为你的百分比加起来是100%?

使用

构建数组
15 times a '1' value, 
15 times a '2' value, 
... 
10 times a '6' value, 
8 times a '7' value,
...
5 times 1 '10' value

你最终会得到一个包含100个元素的数组。

随机选取一个元素(并从数组中弹出)。

答案 1 :(得分:1)

如果您的权重是百分比,请选择0到100之间的随机数,然后迭代地减去百分比,直到您越过零:

<?php
function getWeightedRandom() {
    $weights = array(15, 15, 12, ...); // these should add up to 100
    $r = rand(0, 99);
    for ($i=0; $i<count($weights); $i++) {
        $r -= $weights[$i];
        if ($r < 0)
            return $i+1;
    }
}
?>

这具有支持非整数权重的额外好处。

答案 2 :(得分:1)

使用以下类的OPs权重回显值的示例:

echo 1+Rand::get_weighted_rand(array(15,15,12,12,10,10,8,8,5,5));

和班级:

class Rand
{
    /*
     * generates a random value based on weight
     * @RETURN MIXED: returns the key of an array element
     * @PARAM $a ARRAY:
     *  the array key is the value returned and the array value is the weight
     *      if the values sum up to less than 100 than the last element of the array 
     *      is the default value when the number is out of the range of other values
     * @PARAM $p INT: number of digits after decimal
     *
     * i.e array(1=>20, 'foo'=>80): has an 80 chance of returning Foo
     * i.e array('bar'=>0.5, 2=>1, 'default'=>0), 1: 98.5% chance of returning default
     */
    public static function get_weighted_rand($a, $p=0)
    {
        if(array_sum($a)>100)
            return FALSE;#total must be less than 100
        $p=pow(10, $p+2);
        $n=mt_rand(1,$p)*(100/$p);
        $range=100;
        foreach($a as $k=>$v)
        {
            $range-=$v;
            if($n>$range)
                return $k;
        }
            #returning default value
        end($a);
        return key($a);
    }
}

答案 3 :(得分:0)

将它们全部放在数组中,例如1 15次,3次12次等。 然后从该数组中选择一个随机数。

$array = array_merge (array_fill (0, 15, 1), array_fill (0, 15, 2), array_fill (0, 12, 3), array_fill (0, 12, 4), array_fill (0, 10, 5), array_fill (0, 10, 6), array_fill (0, 8, 7), array_fill (0, 8, 8), array_fill (0, 5, 9), array_fill (0, 5, 10));
$random_number = array_rand ($array);
相关问题