dongzha3058 2015-07-24 13:40
浏览 56
已采纳

为什么mt_rand(1,PHP_INT_MAX)总是返回一个奇数

I've just come across an interesting question from ComputerGuru on Hacker News and no comment seems to give a convincing answer.

Why does mt_rand(1, PHP_INT_MAX) always return an odd number?

I am not the author of the original question.

http://3v4l.org/dMbat

for ($i=0;$i<10000;$i++)
{
    echo mt_rand(1, PHP_INT_MAX)."
";
}

output:

8571620074060775425
7401021871338029057
4351677773593444353
1801559362708176897
7848614552286527489
...
  • 写回答

1条回答 默认 最新

  • dtbiszu7724 2015-07-24 15:41
    关注

    PHP_INT_MAX here is 263-1 (64-bit signed int max).

    However, mt_rand() doesn't handle values this large. The Mersenne twister internally generates 32-bit words, and PHP's mt_getrandmax() is only 231-1 (it throws away the highest bit).

    To generate a value in your requested min to max range, mt_rand first gets the 0 to 231-1 random number, then scales it using this formula:

    x = ((x / (mt_getrandmax() + 1)) * (max - min + 1)) + min;
    

    (See the source of rand.c and php_rand.h.)

    Basically it blindly scales the internally generated number to fit the overlarge range, without even raising a warning. Multiplying to fit the overlarge range generates a lot of zeroes in the low bits, then adding min (which is 1) makes the result odd.

    The problem is more dramatic in hexadecimal, where you can see that the low 32 bits of each number are completely non-random:

    for ($i = 0; $i < 10000; $i++) {
        printf("%016x
    ", mt_rand(1, PHP_INT_MAX));
    }
    

    Output:

    41e0449b00000001
    53d33d7c00000001
    6ec8855700000001
    234140e000000001
    13a4581900000001
    77547beb00000001
    35a0660a00000001
    0d0cd44200000001
    ...
    

    There is a note in the manual that tries to warn about this, although it understates the problem:

    The distribution of mt_rand() return values is biased towards even numbers on 64-bit builds of PHP when max is beyond 232. This is because if max is greater than the value returned by mt_getrandmax(), the output of the random number generator must be scaled up.

    (It says it's biased towards even numbers, but that's only true when min is even.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥500 高有偿提问!求优化设计微信小程序
  • ¥15 matlab在安装时报错 无法找到入口 无法定位程序输入点
  • ¥15 收益高的广告联盟有哪些
  • ¥15 Android Studio webview 的使用问题, 播放器横屏全屏
  • ¥15 删掉jdk后重新下载,Java web所需要的eclipse无法使用
  • ¥15 uniapp正式环境中通过webapi将本地数据推送到设备出现的跨域问题
  • ¥15 xui建立节点,显示错误
  • ¥15 关于#单片机#的问题:开始、复位、十进制的功能可以实现,但是切换八进制的功能无法实现(按下按键也没有效果),把初始状态调成八进制,也是八进制可以实现但是切换到十进制不行(相关搜索:汇编语言|计数器)
  • ¥15 VINS-Mono或Fusion中feature_manager中estimated_depth是特征的深度还是逆深度?
  • ¥15 谷歌浏览器如何备份抖音网页数据