可重现的随机数系列 [英] reproducible random number series
问题描述
如何在PHP中获得一系列可重现的伪随机数?
在旧版本的PHP中,我可以使用 RNG ,但由于PHP更改了rand和mt_rand的工作方式而不再起作用.
也请在PHP.net页面上查看此注释:
请记住,Suhosin修补程序默认情况下安装在 许多PHP安装(例如Debian和DirectAdmin)完全禁用 出于加密安全原因,使用srand和mt_srand函数.到 从一个 Suhosin加固的服务器,您将需要包括自己的伪随机 生成器代码.
该评论的
链接: http://www.php.net/manual/zh-CN/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及更高版本上有效.
How can i get a series of reproducible pseudorandom numbers in PHP?
In older versions of PHP i could do that just by using the same seed in the RNG, but it does not work anymore since PHP has changed the way rand and mt_rand works.
Please also see this comment in PHP.net page:
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.
link of that comment: http://www.php.net/manual/en/function.srand.php#102636
Is there any solution ready? I do not have the time nor the experience to create my own pseudo-random generator code.
My goal is to have a code
<?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;
?>
so everytime i visit this page i should get the same number.
The Mersenne Twist is a nice fast PRNG and here's a public domain PHP implementation for it:
http://kingfisher.nfshost.com/sw/twister/
That only works on PHP 5.3.0 and above.
这篇关于可重现的随机数系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!