|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2013-03-19 01:02 UTC] stefano dot baccianella at gmail dot com
Description: ------------ When using mt_rand function using pow(10,12) as MAX parameter the function seems to get an overflow giving the warning: PHP Warning: mt_rand(): max(-727379968) is smaller than min(0) But the function works perfectly when using greater MAX value like pow(10,13) Below the diff of my php.ini and the php.ini-development provided by default: short_open_tag = On error_reporting = E_ALL & ~E_DEPRECATED & ~E_NOTICE include_path = ".;C:\Program Files (x86)\PHP\pear" extension=php_bz2.dll extension=php_curl.dll extension=php_gd2.dll extension=php_gettext.dll extension=php_mbstring.dll extension=php_mysql.dll extension=php_mysqli.dll extension=php_openssl.dll extension=php_pdo_pgsql.dll extension=php_pgsql.dll extension=php_apc.dll extension=php_sockets.dll ;extension=php_sqlite.dll extension=php_sqlite3.dll date.timezone = Europe/Rome [apc] [APC] apc.enabled = 1 apc.shm_segments = 1 apc.shm_size = 512M apc.num_files_hint = 1000 apc.user_entries_hint = 4096 apc.ttl = 0 apc.user_ttl = 0 apc.gc_ttl = 3600 apc.cache_by_default = 1 apc.filters = "apc\.php$" apc.mmap_file_mask = "/tmp/apc.XXXXXX" apc.slam_defense = 0 apc.file_update_protection = 2 apc.enable_cli = 0 apc.max_file_size = 10M apc.use_request_time = 1 apc.stat = 1 apc.write_lock = 1 apc.report_autofilter = 0 apc.include_once_override = 0 apc.localcache = 0 apc.localcache.size = 256M apc.coredump_unmap = 0 apc.stat_ctime = 0 apc.canonicalize = 1 Test script: --------------- <? echo mt_rand(0,pow(10,12)); echo mt_rand(0,pow(10,13)); ?> Expected result: ---------------- (a random number)(a random number) Actual result: -------------- PHP Warning: mt_rand(): max(-727379968) is smaller than min(0) (a random number) Patchesbug64450.patch (last revision 2013-03-21 20:00 UTC by ab@php.net)Pull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Nov 28 13:00:02 2025 UTC |
Please consider the following snippet while(true) { $max = pow(10, 13); // 10000000000000 $rand = mt_rand(0, $max); if ($rand > PHP_INT_MAX) { break; } } It *seems* to work, but you'll never reach the break condition. On some systems that might be PHP_INT_MAX*2, not sure. Thus, the whole range between pow(10, 13) and PHP_INT_MAX will never be returned. Additionally please be aware - we deal with a pseudo randomness here, it works as expected in 32 bit range only with 32 bit long. The implementation seems to run unexpected overflows on some places like pow(10, 12) you've reported, but it just physically won't work with floats. Thanks for reporting. A better solution is definitely needed for this functionality, not just a bug fix.