php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43171 nonrandom rand() results in lower bits
Submitted: 2007-11-01 13:30 UTC Modified: 2008-10-26 23:44 UTC
From: bdew at druzya dot org Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.2.4 OS: windows xp sp2
Private report: No CVE-ID: None
 [2007-11-01 13:30 UTC] bdew at druzya dot org
Description:
------------
Under windows xp cli version, rand() seems to return cycling values in lower bits.

If i use rand(x,y) the bug does happen only when max_rand+2>y>max_rand+2

mt_rand() is unaffected

Tested same code under linux cli and the bug doesn't happen.

possibly related to http://bugs.php.net/bug.php?id=43108

Reproduce code:
---------------
$a=rand() & 0xff;
for ($k=0;$k<10;$k++) {
	$i=0;
	do {
		$i++;
		$b=rand() & 0xff;
	} while ($a!=$b);
	echo("cycle: $i\n"); 
};

Expected result:
----------------
10 lines with "cycle: x", x being different every line

Actual result:
--------------
cycle: 256
cycle: 256
cycle: 256
cycle: 256
cycle: 256
cycle: 256
cycle: 256
cycle: 256
cycle: 256
cycle: 256

every time

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-11-01 13:54 UTC] bdew at druzya dot org
Err, sory...

"If i use rand(x,y) the bug does happen only when
max_rand+2>y>max_rand+2"

should have read 

"If i use rand(x,y) the bug does happen only when
max_rand+2>y>max_rand-2"
 [2008-10-26 23:44 UTC] jani@php.net
Please do not submit the same bug more than once. An existing
bug report already describes this very problem. Even if you feel
that your issue is somewhat different, the resolution is likely
to be the same. 

Thank you for your interest in PHP.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 21:01:31 2024 UTC