Bug #69396 [NEW]: Strange behavior of rand function with specific big $max value
From: mariusz at mariuszgil dot pl
Operating system: Mac, Linux
PHP version: 5.5.23
Package: *Math Functions
Bug Type: Bug
Bug description:Strange behavior of rand function with specific big $max value
Description:
------------
With specific big $max value, rand(0, $max) function returns unexpected,
strange values.
Test script creates an array with numbers of occurences rand(0, $max) %
$n results. Expected result is an array with all values (almost) equal,
with big $iter value.
But when $max is equal:
- getrandmax() * 2
- getrandmax() * 2 + 1
- getrandmax() * 2 + 2
returned array looks like:
array(16) {
[0]=>
int(0)
[1]=>
int(124837)
[2]=>
int(0)
[3]=>
int(125168)
[4]=>
int(0)
[5]=>
int(124687)
[6]=>
int(0)
[7]=>
int(125015)
[8]=>
int(0)
[9]=>
int(124685)
[10]=>
int(0)
[11]=>
int(125265)
[12]=>
int(0)
[13]=>
int(125077)
[14]=>
int(0)
[15]=>
int(125266)
}
When $max is equal:
- getrandmax() * 2 + 4
- getrandmax() * 2 + 6
- getrandmax() * 2 + 8
- getrandmax() * 2 + 10
- ...
distribution of counters looks very strange, like:
array(16) {
[0]=>
int(83187)
[1]=>
int(41787)
[2]=>
int(83074)
[3]=>
int(41420)
[4]=>
int(83377)
[5]=>
int(41756)
[6]=>
int(83319)
[7]=>
int(41941)
[8]=>
int(83469)
[9]=>
int(41925)
[10]=>
int(83327)
[11]=>
int(41544)
[12]=>
int(83287)
[13]=>
int(41528)
[14]=>
int(83098)
[15]=>
int(41961)
}
Probably, when $max is greater then getrandmax(), an error/notice should
be generated.
Test script:
---------------
<?php
$iter = 1000000;
$max = getrandmax() * 2;
$n = 16;
$values = array_fill(0, $n, 0);
for ($i = 0; $i < $iter; $i++) {
$values[rand(0, $max) % $n] += 1;
}
var_dump($values);
Expected result:
----------------
Expected results of test script is something similiar to:
array(16) {
[0]=>
int(62287)
[1]=>
int(62495)
[2]=>
int(62638)
[3]=>
int(62791)
[4]=>
int(62386)
[5]=>
int(62533)
[6]=>
int(62921)
[7]=>
int(62532)
[8]=>
int(62971)
[9]=>
int(62541)
[10]=>
int(62854)
[11]=>
int(61833)
[12]=>
int(62269)
[13]=>
int(62305)
[14]=>
int(62351)
[15]=>
int(62293)
}
when all values are close to each other.
--
Edit bug report at https://bugs.php.net/bug.php?id=69396&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=69396&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=69396&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=69396&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=69396&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=69396&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=69396&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=69396&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=69396&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=69396&r=support
Expected behavior: https://bugs.php.net/fix.php?id=69396&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=69396&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=69396&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=69396&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=69396&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=69396&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=69396&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=69396&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=69396&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=69396&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=69396&r=mysqlcfg
Thread (3 messages)
- mariusz at mariuszgil dot pl