Edit report at https://bugs.php.net/bug.php?id=63174&edit=1
ID: 63174
Updated by: nikic@php.net
Reported by: ub dot x7b8 at gmail dot com
Summary: Magic negative number generated by rand
-Status: Open
+Status: Closed
Type: Bug
Package: *Math Functions
Operating System: 32bit
PHP Version: Irrelevant
-Assigned To:
+Assigned To: nikic
Block user comment: N
Private report: N
New Comment:
Biases in mt_rand() have been fixed in PHP 7.1, with a remaining issue fixed in PHP 7.2. As such,
this is now resolved.
Previous Comments:
------------------------------------------------------------------------
[2015-08-15 10:23:49] cmb@php.net
Related To: Bug #60543
------------------------------------------------------------------------
[2015-07-15 22:49:49] cmb@php.net
> This problem only manifests when there's overflow on 32-bit> platforms.
ACK. However, on 64bit platforms there's a closely related problem
if $max-$min > PHP_INT_MAX, in which case rand() unevenly
distributes, e.g. running
<?php
for ($i = 0; $i < 1000000; $i++) {
if (rand(PHP_INT_MIN, PHP_INT_MAX) > 0) {
echo "Never\n";
break;
}
}
?>
never prints "Never". The problem is in RAND_RANGE() where only
the offset is calculated as double, instead of the whole
expression (see the attached patch "rand-range-double"). This
change would also fix the behavior on 32bit architectures.
However, changing the results of (mt_)rand() for $max-$min >
PHP_INT_MAX and with regard to inexact double to integer
conversion on 64bit architectures would be a BC break, so I'm not
sure whether we should apply that for PHP 5. IMHO, changing PHP 7
in this regard would be acceptable even though it's in feature
freeze.
Anyway, I would suggest to better document the behavior for non
default ranges.
------------------------------------------------------------------------
[2015-07-15 22:32:49] cmb@php.net
The following patch has been added/updated:
Patch Name: rand-range-double
Revision: 1436999569
URL: https://bugs.php.net/patch-display.php?bug=63174&patch=rand-range-double&revision=1436999569
------------------------------------------------------------------------
[2015-07-13 20:06:48] bishop@php.net
https://github.com/php/php-src/pull/1416
------------------------------------------------------------------------
[2015-07-09 16:12:04] bishop@php.net
Duplicated by #70003.
------------------------------------------------------------------------
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=63174
--
Edit this bug report at https://bugs.php.net/bug.php?id=63174&edit=1