Bug #63174 [Asn]: Magic negative number generated by rand

From: Date: Thu, 09 Jul 2015 16:12:04 +0000
Subject: Bug #63174 [Asn]: Magic negative number generated by rand
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-194260@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=63174&edit=1

 ID:                 63174
 Updated by:         bishop@php.net
 Reported by:        ub dot x7b8 at gmail dot com
 Summary:            Magic negative number generated by rand
 Status:             Assigned
 Type:               Bug
 Package:            *Math Functions
 PHP Version:        Irrelevant
 Assigned To:        bishop
 Block user comment: N
 Private report:     N

 New Comment:

Duplicated by #70003.


Previous Comments:
------------------------------------------------------------------------
[2015-07-09 15:52:03] bishop@php.net

@yohgaki That's because 3v4l runs 64-bit: http://3v4l.org/a8KaY

This problem only manifests when there's overflow on 32-bit platforms.

------------------------------------------------------------------------
[2015-07-08 21:50:55] yohgaki@php.net

It seems working well on 3v4l.
http://3v4l.org/BqqSr

------------------------------------------------------------------------
[2015-07-07 10:55:14] cmb@php.net

Related To: Bug #70003

------------------------------------------------------------------------
[2012-09-27 16:38:48] 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


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=63174&edit=1


Thread (11 messages)

« previous php.bugs (#194260) next »