是否可以在PHP中预测rand(0,10)? [英] Is it possible to predict rand(0,10) in PHP?
问题描述
我有一个脚本,可以在PHP中使用rand函数.现在,我读了一些鬼故事,它们确实很容易预测这些结果.从客户端这可能吗?
I have a script where I use the rand function in PHP. Now I read some ghost stories that its real easy to predict those outcomes. Is this possible from the client-side?
例如,假设我们有一个rand(0,10)
.可以预测下一个数字吗?
For example, let say we have a rand(0,10)
. Is it possible to predict the next number?
推荐答案
rand()
函数返回伪随机数.
这并不意味着可以预测next
数.
但是,此图像可以解释单词pseudorandom
rand()
function returns a pseudorandom number .
This does NOT mean that the next
number can be predicted.
However this image can explain the concept of word pseudorandom
您可以阅读文章
图像是通过Windows系统上具有rand
功能的简单循环生成的.
the image is generated from a simple loop with rand
function on windows system.
header("Content-type: image/png");
$im = imagecreatetruecolor(512, 512) or die("Cannot Initialize new GD image stream");
$white = imagecolorallocate($im, 255, 255, 255);
for ($y = 0; $y < 512; $y++) {
for ($x = 0; $x < 512; $x++) {
if (rand(0, 1)) {
imagesetpixel($im, $x, $y, $white);
}
}
}
imagepng($im); imagedestroy($im);
不是那么随机,真的吗?但是现在您知道了...您可以预测下一个数字了吗?
It's not so random, really? But now that you know it... you can predict the next number?
真随机数生成器(TRNG)与伪随机数生成器(PRNG)之间的区别在于,TRNG使用不可预测的物理方式生成数字(例如大气噪声),而PRNG使用数学算法(完全由计算机生成)
The difference between true random number generators (TRNGs) and pseudo-random number generators (PRNGs) is that TRNGs use an unpredictable physical means to generate numbers (like atmospheric noise), and PRNGs use mathematical algorithms (completely computer-generated)
[...]
很少有PRNG会产生这种明显的视觉图案,恰好碰巧是语言(PHP),操作系统(Windows)和函数(rand())的真正糟糕组合.
Not many PRNGs will produce an obvious visual pattern like this, it just so happens to be a really bad combination of language (PHP), operating system (Windows), and function (rand()).
这篇关于是否可以在PHP中预测rand(0,10)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!