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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: bdew at druzya dot org
New email:
PHP Version: OS:

 

 [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: Fri Apr 26 08:01:30 2024 UTC