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

WBOY
Release: 2016-06-06 20:39:01
Original
1430 people have browsed it

<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>
Copy after login
Copy after login

结果是

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

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

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

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

回复内容:

<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>
Copy after login
Copy after login

结果是

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

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

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

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

把srand放到循环外

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

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

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

你的問題本身就是錯的。

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template