#27052 [Opn]: rand() returns easily predictable numbers

From: Date: Mon, 26 Jan 2004 20:56:22 +0000
Subject: #27052 [Opn]: rand() returns easily predictable numbers
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-53917@lists.php.net to get a copy of this message
 ID:               27052
 User updated by:  webmaster at armondc dot com
 Reported By:      webmaster at armondc dot com
 Status:           Open
 Bug Type:         Unknown/Other Function
 Operating System: Windows 2000 Professional
 PHP Version:      4.3.4
 New Comment:

On the "actual result" its not as easily spotted without wrapping. 
This should give a better picture:

]F[LIBGHU^SDAZ_MVK\YRWXENCTQJOP]F[LIBGHU^SDAZ_MVK\YRWXENCTQJOP]F
[LIBGHU^SDAZ_MVK\YRWXENCTQJOP]F[LIBGHU^SDAZ_MVK\YRWXENCTQJOP]F
[LIBGHU^SDAZ_MVK\YRWXENCTQJOP]F[LIBGHU^SDAZ_MVK\YRWXENCTQJOP]F
[LIBGHU^SDAZ_MVK\YRWXENCTQJOP]F[LIBGHU^SDAZ_MVK\YRWXENCTQJOP]F
[LIBGHU^SDAZ_MVK\YRWXENCTQJOP]F[LIBGHU^SDAZ_MVK\YRWXENCTQJOP]F
[LIBGHU^SDAZ_MVK\YRWXENCTQJOP]F[LIBGHU^SDAZ_MVK\YRWXENCTQJOP]F
[LIBGHU^SDAZ_MVK\YRWXENCTQJOP]F[LIBGHU^SDAZ_MVK\YRWXENCTQJOP]F
[LIBGHU^SDAZ_MVK\YRWXENCTQJOP]F[LIBGHU^SDAZ_MVK\


Previous Comments:
------------------------------------------------------------------------

[2004-01-26 15:53:26] webmaster at armondc dot com

Description:
------------
I'm not sure if this is a very major problem, or even a bug, but here
goes:

It seems rand() on Windows is very predictable.

I've tried it on 2 Windows Systems, and they were both predictable.  It
showed a breif unpredictable segment, then followed by the same string
of characters over and over, and finally a few more unpredictable
characters.

I tried this on a FreeBSD 5.2 System, and it was unpreditable enough. 
Seems to be a windows-only problem.

I also noticed bug#11108 - It seems similar, but the submitter was
using his own seeds, and I am using without.

Reproduce code:
---------------
<?php
// This will reproduce the problem
// on windows
for($x = 0; $x < 500; $x++) {
  echo chr((rand()%32)+65);
} 

// This is a quick-fix, but
// goes agaist the documentation's
// 'no need for srand()'
for($x = 0; $x < 500; $x++) {
	srand(hexdec(substr(md5(microtime()), 0, 8)));
	echo chr((rand()%32)+65);
}

?>

Expected result:
----------------
Something a bit random like:

HEOSITFS[ZYH_EVIVVXSLCWRHHC_]_L^PIBTC\TXPBWQLCM^VZSUUHOG[\VF[KCDGFZM^Q\IG^KAIYPIYBSBIMGHXIBMPHLY\OAZMYAZPJ_DF\X\QXR[GP]DQLRHMYIOERBMEKBDJMJPN]J\\KPES^EGRKKO^S[XL\SSHQXOYQU_JDMMZEUEOQOYMHJPYDTRRU_URHYJGXRQYDYADD]OBVUGC^GGSXPRJJAOAKRAYYN^Q\XFQJNOWQKAJBFONFXNDZFFIMYRLROH^]IM_MINJI\MJJAVG]_B_EAVC\O_\DHCHLX_NIOU^KMJF\K]VZDOZ_FAWCZC_IQK^[NTPZCC]XEFZ[\AG[PV^XLPRYVAS]ZPH^WAXYAZYGK_M_QKF]PRMP^BBPTRV^KV_VDOLFIM]WQNHVF^DYWGGSGLJ_ZMODG[Z_AAXBYD^FS\MITLZESC`DXLH]NWNIVWZZMVMSXFQMG[O[W[YSPVMTQLHD

Actual result:
--------------
An easily spotted pattern:

]F[LIBGHU^SDAZ_MVK\YRWXENCTQJOP]F[LIBGHU^SDAZ_MVK\YRWXENCTQJOP]F[LIBGHU^SDAZ_MVK\YRWXENCTQJOP]F[LIBGHU^SDAZ_MVK\YRWXENCTQJOP]F[LIBGHU^SDAZ_MVK\YRWXENCTQJOP]F[LIBGHU^SDAZ_MVK\YRWXENCTQJOP]F[LIBGHU^SDAZ_MVK\YRWXENCTQJOP]F[LIBGHU^SDAZ_MVK\YRWXENCTQJOP]F[LIBGHU^SDAZ_MVK\YRWXENCTQJOP]F[LIBGHU^SDAZ_MVK\YRWXENCTQJOP]F[LIBGHU^SDAZ_MVK\YRWXENCTQJOP]F[LIBGHU^SDAZ_MVK\YRWXENCTQJOP]F[LIBGHU^SDAZ_MVK\YRWXENCTQJOP]F[LIBGHU^SDAZ_MVK\YRWXENCTQJOP]F[LIBGHU^SDAZ_MVK\YRWXENCTQJOP]F[LIBGHU^SDAZ_MVK\


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


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


Thread (6 messages)

« previous php.bugs (#53917) next »