Edit report at https://bugs.php.net/bug.php?id=64450&edit=1
ID: 64450 User updated by: stefano dot baccianella at gmail dot com 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: Yeah, true. I was misled by the fact that my system is 64bit but PHP for windows is compiled on 32bit. I think that an RFC for 5.6 should be enough. Thank you Previous Comments: ------------------------------------------------------------------------ [2013-03-25 23:22:53] a...@php.net I'm not sure something like that would be accepted as the valid domain is clearly documented and such a change is still a BC breach. What might work is a change request including an RFC for 5.6, otherwise a PECL ext. ------------------------------------------------------------------------ [2013-03-25 20:47:13] stefano dot baccianella at gmail dot com Ok, now it's clear. Maybe as a minor fix I can suggest to check if MAX is greater than PHP_INT_MAX and return a value instead of an error (FALSE value?) or an error with an explanation, in both cases it's better than an overflow. Thank you very much for your time ------------------------------------------------------------------------ [2013-03-25 20:40:52] a...@php.net 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. ------------------------------------------------------------------------ [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). ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=64450 -- Edit this bug report at https://bugs.php.net/bug.php?id=64450&edit=1