Heim  >  Artikel  >  Backend-Entwicklung  >  mt_rand - php 随机函数 实际概率和设置的概率有差异

mt_rand - php 随机函数 实际概率和设置的概率有差异

WBOY
WBOYOriginal
2016-06-06 20:39:011397Durchsuche

<code>php</code><code><?php $result = array();
$configSet = array(
    6 => 0.1,
    5 => 1,
    1 => 27,
    2 => 27,
    3 => 27,
    4 => 17.9,
);
$nums = 30000000;
for($i=1;$i $value){
        $rand += $value * 10000;
        if($random  $value){
    $result[$key] = round((float)($value / $nums) * 1000000)/10000;
}
ksort($result);
print_r($result);
?>
</code>

结果是

<code>Array
(
    [1] => 26.985
    [2] => 26.9879
    [3] => 26.987
    [4] => 17.9519
    [5] => 0.9917
    [6] => 0.0964
)
</code>

跑了很多次都是差不多的,
实际用户10000W次结果就有点问题

<code>Array
(
    [1] => 27.0581
    [2] => 27.2639
    [3] => 27.2125
    [4] => 18.1036
    [5] => 0.3031
    [6] => 0.0588
)
</code>

怎么能保证小概率的时候波动小一点

回复内容:

<code>php</code><code><?php $result = array();
$configSet = array(
    6 => 0.1,
    5 => 1,
    1 => 27,
    2 => 27,
    3 => 27,
    4 => 17.9,
);
$nums = 30000000;
for($i=1;$i $value){
        $rand += $value * 10000;
        if($random  $value){
    $result[$key] = round((float)($value / $nums) * 1000000)/10000;
}
ksort($result);
print_r($result);
?>
</code>

结果是

<code>Array
(
    [1] => 26.985
    [2] => 26.9879
    [3] => 26.987
    [4] => 17.9519
    [5] => 0.9917
    [6] => 0.0964
)
</code>

跑了很多次都是差不多的,
实际用户10000W次结果就有点问题

<code>Array
(
    [1] => 27.0581
    [2] => 27.2639
    [3] => 27.2125
    [4] => 18.1036
    [5] => 0.3031
    [6] => 0.0588
)
</code>

怎么能保证小概率的时候波动小一点

把srand放到循环外

實際概率和設置概率有差異。。。

你怎麼知道實際概率是多少的?

你知道的只是頻率好不好。。。

你的問題本身就是錯的。

而頻率和概率不等幾乎是必然的。。。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn