Rumah  >  Artikel  >  pembangunan bahagian belakang  >  mt_rand - php 随机函数 实际概率和设置的概率有差异

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

WBOY
WBOYasal
2016-06-06 20:39:011398semak imbas

<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放到循环外

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

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

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

你的問題本身就是錯的。

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

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn