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条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?