From:             ub dot x7b8 at gmail dot com
Operating system: 
PHP version:      Irrelevant
Package:          *Math Functions
Bug Type:         Bug
Bug description:Magic negative number generated by rand

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

-- 
Edit bug report at https://bugs.php.net/bug.php?id=63174&edit=1
-- 
Try a snapshot (PHP 5.4):   
https://bugs.php.net/fix.php?id=63174&r=trysnapshot54
Try a snapshot (PHP 5.3):   
https://bugs.php.net/fix.php?id=63174&r=trysnapshot53
Try a snapshot (trunk):     
https://bugs.php.net/fix.php?id=63174&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=63174&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=63174&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=63174&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=63174&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=63174&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=63174&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=63174&r=notwrong
Not enough info:            
https://bugs.php.net/fix.php?id=63174&r=notenoughinfo
Submitted twice:            
https://bugs.php.net/fix.php?id=63174&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=63174&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=63174&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=63174&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=63174&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=63174&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=63174&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=63174&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=63174&r=mysqlcfg

Reply via email to