3

假设我想随机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

4

4 回答 4

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 个元素的数组。

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

于 2012-05-11T21:54:49.990 回答
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;
    }
}
?>

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

于 2012-05-11T21:57:09.290 回答
1

具有以下类的 OP 权重的示例回显值:

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);
    }
}
于 2012-05-11T21:57:34.867 回答
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);
于 2012-05-11T21:55:31.393 回答