我有一个脚本,我在其中使用了 PHP 中的 rand 函数。现在我读了一些鬼故事,说预测这些结果真的很容易。这可能来自客户端吗?
例如,假设我们有一个 rand(0,10)
。是否可以预测下一个数字?
最佳答案
rand()
函数返回一个伪随机数。
这并不意味着可以预测 next
号码。
然而这张图片可以解释单词 pseudorandom
你可以阅读这个article
图像是在 Windows 系统上使用 rand
函数从一个简单的循环生成的。
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);
这不是随机的,真的吗?但现在您知道了……您可以预测下一个数字吗?
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)
[...]
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 - 是否可以在 PHP 中预测 rand(0,10)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12729459/