|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-09-27 16:38 UTC] ub dot x7b8 at gmail dot com
Description:
------------
rand ( $min, getrandmax());
if $min < 0 then function generates number = getrandmax () - abs ( $min ) + 1 with very high probability.
REPRODUCED: PHP 5.2.6-1 Debian 5.0.10 i386 under VmWare
REPRODUCED: PHP 5.2.6-1 Debian 5.0.5 i386
REPRODUCED: PHP 5.2.3, 5.2.8, 5.2.9, 5.4.7 Debian 6.0.4 i386 under VirtualBox
NOT REPRODUCED: PHP 5.2.1 Solaris 5.10 sparc
NOT REPRODUCED: PHP 5.2.9 Solaris 5.10 sparc
NOT REPRODUCED: PHP 4.4.4-8 Debian 4.0 i386 under VmWare
Test script:
---------------
<?php
$rand_min = -getrandmax (); # 2137483647
$rand_max = getrandmax ();
$c = 1000;
for ( $min = $rand_min; $min < 0; $min+=10000000 ) {
$diff = $rand_max - abs ( $min ) + 1;
$cnt = 0;
for ( $i = 0; $i < $c; $i++ ) {
$super_random_value = rand ( $min, $rand_max );
if ( $super_random_value == $diff ) $cnt++;
}
$per = $cnt * 100 / $c;
echo "Number '$diff' was generated $cnt times, " . $per . "%, minimum = $min\n";
}
?>
Expected result:
----------------
Number '1' was generated 0 times, 0%, minimum = -2147483647
Number '10000001' was generated 0 times, 0%, minimum = -2137483647
Number '20000001' was generated 0 times, 0%, minimum = -2127483647
Number '30000001' was generated 0 times, 0%, minimum = -2117483647
Number '40000001' was generated 0 times, 0%, minimum = -2107483647
...skipped....
Number '2100000001' was generated 0 times, 0%, minimum = -47483647
Number '2110000001' was generated 0 times, 0%, minimum = -37483647
Number '2120000001' was generated 0 times, 0%, minimum = -27483647
Number '2130000001' was generated 0 times, 0%, minimum = -17483647
Number '2140000001' was generated 0 times, 0%, minimum = -7483647
Actual result:
--------------
Number '1' was generated 514 times, 51.4%, minimum = -2147483647
Number '10000001' was generated 507 times, 50.7%, minimum = -2137483647
Number '20000001' was generated 522 times, 52.2%, minimum = -2127483647
Number '30000001' was generated 520 times, 52%, minimum = -2117483647
Number '40000001' was generated 502 times, 50.2%, minimum = -2107483647
...skipped...
Number '2100000001' was generated 12 times, 1.2%, minimum = -47483647
Number '2110000001' was generated 14 times, 1.4%, minimum = -37483647
Number '2120000001' was generated 10 times, 1%, minimum = -27483647
Number '2130000001' was generated 11 times, 1.1%, minimum = -17483647
Number '2140000001' was generated 2 times, 0.2%, minimum = -7483647
Patchesrand-range-double (last revision 2015-07-15 22:32 UTC by cmb@php.net)Pull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 05:00:01 2025 UTC |
> 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.