php中rand和mt_rand的区别_php – mt_rand()和rand()之间的区别

更新(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(习惯?)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值