如何在PHP中获得一系列可重现的伪随机数?
在旧版本的PHP中,我可以通过使用相同的种子来实现 RNG,但它不再起作用,因为PHP改变了rand和mt_rand的工作方式。
请在PHP.net页面中看到此评论:
请记住默认情况下安装的Suhosin补丁 许多PHP安装,如Debian和DirectAdmin完全禁用 srand和mt_srand函数用于加密安全性原因。至 从一个固定的种子生成可重现的随机数 Suhosin强化服务器,您需要包含自己的伪随机数 发电机代码。
该评论的链接: 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;
?>
所以每次我访问这个页面我都会得到相同的号码。