|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2003-09-23 18:21 UTC] adamm at nycap dot rr dot com
Description: ------------ I am developing on a newly setup Windows XP Box running PHP 4.3.3 on IIS as CGI. The rand() function is not generating random numbers. Even with a min/max declared it will only once generate a number in that range... and than any other time called (even across browser sessions) it will always and forever generate the same number. The code below is broken down to the smallest example possible. Reproduce code: --------------- <?php echo rand(); ?> Expected result: ---------------- I expect to see a random number. Actual result: -------------- The same number is always displayed. PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Nov 04 17:00:01 2025 UTC |
I was having this problem (PHP 4.3.3 on WinNT 4) but in response to iliaa@php.net's question, the function 'srand()' does indeed fix the problem. The code: for ($j=1; $j<10001; $j++) { print (rand(0, $j)."<br>"); } ...produced the same ten thousand random numbers each and every time. Putting srand() before the loop stopped this happening. I hope this info is useful to someone; not sure anyone will read this...! Vaughany.