php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #37528 rand() weakness
Submitted: 2006-05-19 20:26 UTC Modified: 2006-05-19 21:00 UTC
From: lunter at interia dot pl Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.1.4 OS: WinXP
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: lunter at interia dot pl
New email:
PHP Version: OS:

 

 [2006-05-19 20:26 UTC] lunter at interia dot pl
Description:
------------
rand() weakness.

example (reproduce code): rand generate pattern instead of noise

Reproduce code:
---------------
<?
 $i=imagecreatetruecolor(1024,768);

 for($y=0;$y<768;$y++){
  for($x=0;$x<1024;$x++){
   $c=rand(0,1)*255;
   imagesetpixel($i,$x,$y,($c<<16)+($c<<8)+$c);
  }
 }

 header('Content-type: image/png');
 imagepng($i);
?>

Expected result:
----------------
noise

Actual result:
--------------
pattern

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-05-19 20:35 UTC] lunter at interia dot pl
e.g. MatLab rand() generate perfect noise.
 [2006-05-19 20:37 UTC] pajoye@php.net
Use mt_rand if you want better random values.
 [2006-05-19 20:49 UTC] lunter at interia dot pl
Sorry but..
run this script on Linux PHP - perfect noise
run this script on WinXP PHP - pattern!

WinXP rand() should be good like Linux rand()
Actually rand() is not the same.
 [2006-05-19 20:54 UTC] pajoye@php.net
They are system dependent, it is not a php problem. Now please keep this bug as bogus and read the manual. 
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Mar 12 11:01:32 2025 UTC