简体   繁体   English

PHP mt_rand 每次都返回相同的数字

[英]PHP mt_rand return same number every time

random.php随机文件

$min=1;
$max=13031;
$ran_num = mt_rand($min,$max);
echo $ran_num;

This is working fine on my local host, but when I run it on my server, it always return the same number.这在我的本地主机上运行良好,但是当我在我的服务器上运行它时,它总是返回相同的数字。 not sure what is going on.不知道发生了什么。

Update: I am running a wordpress site, and I put the random.php in the theme folder.更新:我正在运行一个 wordpress 站点,并将 random.php 放在主题文件夹中。 I also have eaccelerator installed, this might be the issue, I am looking into it now我也安装了 eaccelerator,这可能是问题所在,我现在正在研究它

如果您的服务器 PHP 低于 4.2 - 您需要先调用mt_srand才能使用 mt_rand

By default modern PHP creates different number for mt_rand (it's uses libc random number generator), so the problem can be somewhere else.默认情况下,现代 PHP 为mt_rand创建不同的数字(它使用 libc 随机数生成器),因此问题可能出在其他地方。

This can likely to be caused by some caching, for example check the followings:这可能是由某些缓存引起的,例如检查以下内容:

  • clear your web browser caching (use private mode or use different browser),清除您的网络浏览器缓存(使用私有模式或使用不同的浏览器),
  • disable Varnish (if it's in use),禁用清漆(如果正在使用),
  • check whether code is using memory caching like Memcached/Redis,检查代码是否使用内存缓存,如 Memcached/Redis,
  • this may be related to PHP cache accelerators (but less likely),这可能与 PHP 缓存加速器有关(但可能性较小),
  • restart your web server just in case (could be some extra caching module),重启你的网络服务器以防万一(可能是一些额外的缓存模块),
  • make sure you're editing the right file and it was deployed properly,确保您正在编辑正确的文件并且已正确部署,
  • test it in the smallest possible PHP code.用尽可能小的 PHP 代码测试它。

Wordpress may use cache, and if you reload the page, it loads from cache, so you will see the same random number again and again. Wordpress 可能会使用缓存,如果您重新加载页面,它会从缓存中加载,因此您将一次又一次地看到相同的随机数。 It has nothing to do with PHP version in this particular case - to fix this, it is needed to disable the wordpress cache.在这种特殊情况下,它与 PHP 版本无关 - 要解决此问题,需要禁用 wordpress 缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM