php - 是否可以在 PHP 中预测 rand(0,10)?

标签 php math

我有一个脚本,我在其中使用了 PHP 中的 rand 函数。现在我读了一些鬼故事,说预测这些结果真的很容易。这可能来自客户端吗?

例如,假设我们有一个 rand(0,10)。是否可以预测下一个数字?

最佳答案

rand() 函数返回一个伪随机数。 这并不意味着可以预测 next 号码。 然而这张图片可以解释单词 pseudorandom

的概念

rand

你可以阅读这个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/

相关文章:

php - 尽管数据库用户名和密码正确,但登录脚本无法正常工作

algorithm - react 扩散平行生长法

objective-c - Objective C/求解数学系统

c++ - 检查数字是否达到阈值

php - 连接多个表并根据第一个表 ID 获取状态

php - 我应该采取哪些步骤来安全地管理和验证所有 PHP 页面上的 session ?

php MQTT 订阅不工作

php - Zend Framework 切换到本地命名空间

javascript - 从特定来源缩放 Canvas

math - 当有三个项时应用主定理?