douyi5961 2014-08-27 18:27
浏览 1151
已采纳

警告:mt_rand():max(-1)小于min(1)

The following script provides the following output. I am running PHP Version 5.5.12 with Apache/2.2.15 (CentOS) on i386 server. Note I run the same script on my almost identical server which is x86_64, and I don't experience the error.

<?php
$id=mt_rand ( 1 , 4294967295);
?>

Warning: mt_rand(): max(-1) is smaller than min(1) in /var/www/html/testing/mt_rand.php on line 2

What causes this error, and how do I fix it?

  • 写回答

2条回答 默认 最新

  • dongxiangxie8181 2014-08-27 18:35
    关注

    You happen to use a value that is bigger than the integer maximum. As read in What is the maximum value for a int32?, this value is 2,147,483,647.

    From PHP.net in mt_rand():

    Description

    int mt_rand ( void )

    int mt_rand ( int $min , int $max )

    Return Values

    A random integer value between min (or 0) and max (or mt_getrandmax(), inclusive), or FALSE if max is less than min.

    From the PHP manual:

    The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). PHP does not support unsigned integers. Integer size can be determined using the constant PHP_INT_SIZE, and maximum value using the constant PHP_INT_MAX since PHP 4.4.0 and PHP 5.0.5.

    Your script fails on 32 bits systems because you are providing a value which is ~4 billion, while the maximum is ~2 billion. A workaround can be to change PHP_INT_MAX.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘