假設我想choose
隨機數爲1-10
,但每個數字都有權重。PHP選擇隨機數與權重
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
?
假設我想choose
隨機數爲1-10
,但每個數字都有權重。PHP選擇隨機數與權重
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
?
我假設你的百分比合計爲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次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);
如果你的權重百分比,挑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;
}
}
?>
這具有支持非整數的額外好處權重。
示例呼應使用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);
}
}
你可以在1使用標準的分佈(高斯)算法,平均,但ChristopheD的答案要簡單得多。 – Vulcan