加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 可再现的随机数字序列

发布时间:2020-12-13 16:02:36 所属栏目:PHP教程 来源:网络整理
导读:如何在 PHP中获得一系列可重现的伪随机数? 在旧版本的PHP中,我可以通过在RNG中使用相同的种子来实现,但是由于PHP已经改变了rand和mt_rand的工作方式,所以不再工作了. 请在PHP.net页面看到这个评论: Keep in mind that the Suhosin patch which is installe
如何在 PHP中获得一系列可重现的伪随机数?

在旧版本的PHP中,我可以通过在RNG中使用相同的种子来实现,但是由于PHP已经改变了rand和mt_rand的工作方式,所以不再工作了.

请在PHP.net页面看到这个评论:

Keep in mind that the Suhosin patch which is installed by default on
many PHP-installs such as Debian and DirectAdmin completely disables
the srand and mt_srand functions for encryption security reasons. To
generate reproducible random numbers from a fixed seed on a
Suhosin-hardened server you will need to include your own pseudorandom
generator code.

该评论的链接:http://www.php.net/manual/en/function.srand.php#102636

是否有任何解决方案准备好了?我没有时间和经验来创建我自己的伪随机生成器代码.

我的目标是要有一个代码

<?php
   //( pseudo random code here...)
   $the_seed = 123; // 123 is just a number for demo purposes,NOT a static number
                    //...i hope you get the idea. It's just a hardcoded seed,// it could be a seed based on a user-id,a date etc...
                    // we need the same output for a given seed.
   //( pseudo random code here...)

   // ...and finally
   echo $the_random_number;
 ?>

所以每次我访问这个页面,我应该得到相同的数字.

Mersenne Twist是一个很好的快速PRNG,这里是一个公共领域的PHP实现:

http://kingfisher.nfshost.com/sw/twister/

这仅适用于PHP 5.3.0及更高版本.另一个选项,如Sharky所说,与早期版本的PHP更兼容:

http://boxrefuge.com/?p=18

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读