更新(PHP 7.1):
07000 This means that the output for the following functions have changes: rand(), shuffle(), str_shuffle(), and array_rand().
这意味着自7.1版以来,两者都没有实际的区别。
之前PHP 7.1:
在实践中,两个函数(PHP-C包装开销?)之间没有太大的区别。
PHP测试代码:
$sum = 0.0;
for ($i = 0; $i < 100000000; $i++) {
$sum += mt_rand();
}
echo "$sum\n";
?>
连续两次处理mt_rand和rand。
redstar@e305243:/tmp$ time php test.php
1.0737079861567E+17
real 0m9.039s
user 0m9.005s
sys 0m0.004s
redstar@e305243:/tmp$ time php test.php
1.0736957393558E+17
real 0m9.110s
user 0m9.013s
sys 0m0.068s
redstar@e305243:/tmp$ time php test.php
1.0737408525492E+17
real 0m9.499s
user 0m9.457s
sys 0m0.004s
redstar@e305243:/tmp$ time php test.php
1.0737585104647E+17
real 0m9.562s
user 0m9.509s
sys 0m0.024s
但是在数学上,mt_rand比rand更熵。有很多关于这个的文件。在一些应用程序中,我使用mt_rand,但通常我使用rand(习惯?)。