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:
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
Previous Comments:
------------------------------------------------------------------------
[2013-03-25 20:40:52] ab@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] ab@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] ab@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] ab@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
------------------------------------------------------------------------
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