【发布时间】:2012-07-17 22:58:28
【问题描述】:
在我的程序中,$integers[0]->occurrences 的平均值应该是 20000。为什么它总是大于 20000,即使使用了 srand()?
<?php
define('NUM_OF_INTS',49);
define('DRAWS',1000000);
class number
{
public $occurences;
public $value;
function number()
{
$occurences=0;
$value=0;
}
}
$integers = array();
//srand(time(0));
//initialising loop
for($i=0;$i<=NUM_OF_INTS;$i++)
{
$integers[$i] = new number();
$integers[$i]->value = $i;
}
for($i=0;$i<DRAWS;$i++)
{
$integers[rand(0,NUM_OF_INTS)]->occurences++;
}
foreach($integers as $int)
printf("%5d %5d <br/>",$int->value,$int->occurences);
?>
【问题讨论】:
-
你是如何计算平均值的?它必须是(根据定义)20000,因为您将 1000000 次试验分成 50 个箱。
-
总是高一点:20018 , 20028 , 20020 ,20022 ,20021 ,20017...
-
不按此:ideone.com/uZdNF.
-
所以,这似乎是PHP解析器的问题。我只用 WAMP 尝试了这段代码。
-
忽略我上面说的;我误读了您的问题(我以为您是在谈论 跨 箱的平均值,而不是在多次运行中某个特定箱的平均值)。