Edit report at https://bugs.php.net/bug.php?id=64450&edit=1

 ID:                 64450
 Updated by:         a...@php.net
 Reported by:        stefano dot baccianella at gmail dot com
 Summary:            mt_rand causes overflow within certain max value
 Status:             Wont fix
 Type:               Bug
 Package:            *General Issues
 Operating System:   Windows 7 64bit
 PHP Version:        5.4.13
 Assigned To:        ab
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2013-03-25 20:03:14] stefano dot baccianella at gmail dot com

I'm sorry but I don't agree with your explanation. 
If your explanation was valid the test mt_rand(0, 
pow(10,13)) would have failed but in fact is working.

------------------------------------------------------------------------
[2013-03-25 19:53:00] a...@php.net

Reason: http://news.php.net/php.internals/66806

The function is documented to be able working in the int range. Except you've  
platform having true 64 bit long (which x64 windows is not), it's limited 
values 
within PHP_INT_MAX and (PHP_INT_MAX-1).

------------------------------------------------------------------------
[2013-03-21 20:00:06] a...@php.net

The following patch has been added/updated:

Patch Name: bug64450.patch
Revision:   1363896006
URL:        
https://bugs.php.net/patch-display.php?bug=64450&patch=bug64450.patch&revision=1363896006

------------------------------------------------------------------------
[2013-03-21 14:03:29] a...@php.net

The following patch has been added/updated:

Patch Name: bug64450.patch
Revision:   1363874609
URL:        
https://bugs.php.net/patch-display.php?bug=64450&patch=bug64450.patch&revision=1363874609

------------------------------------------------------------------------
[2013-03-19 01:02:13] 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)


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=64450&edit=1

Reply via email to