Page 1 of 1

apache caches php random numbers?

PostPosted: 02. September 2016 13:20
by mishamax
I'm not sure what is going on... all i know is that i'm getting same random numbers set after refreshing the page even when i shouldn't have to. here is the php code:
i took this example from codecademy.com php lessons Looping the Loop.

<!DOCTYPE html>
<html>
<head>
<title>Coin Flips</title>
</head>
<body>
<p>We are going to flip a coin until we get three heads in a row!</p>
<?php
$headCount = 0;
$flipCount = 0;
while ($headCount < 13) {
$flip = rand(0,1);
$flipCount ++;
if($flip) {
$headCount ++;
// echo "<div class=\"coin\">H</div>";
}
else{
$headCount = 0;
//echo "<div class=\"coin\">T</div>";
}
}
echo "<p>It took {$flipCount} flips!</p>";
?>
</body>
</html>
at codecademy's compilator this code always gives me different answers but when i'm trying to do this with xampp i'm getting same numbers or set of same numbers after keep refreshing and i don't want it to be that way.
so the answer keeps repeating: It took 55 flips! than comes, It took 747 flips! 3649, 7072, 622, 1875 and all keeps repeating with the same order all the time if i refresh pages. i think it's catched values at xampp? I launched this script with chrome and firefox and i'm having same results same numbers and same order. so my random values not works and i'm very sad about this. i even have deleted whole browser cache history and still same results. any help? is my explanation clear enough? thanks!

Re: apache caches php random numbers?

PostPosted: 02. September 2016 18:38
by JJ_Tagy
I have no issues with the code on XAMPP programmatically. Logically, you are looking for 13 heads in a row rather than the 3 stated.

Re: apache caches php random numbers?

PostPosted: 03. September 2016 12:46
by Nobbie
If you need to have different random followups, you must use srand() function to initialize the random functions (use a timestamp for example for srand() to generate different values). If you do not initialize random functions by srand(), the behaviour depends on memory trash and depending on the machine it may always result in the same random numbers.

On my Linux machine your program works as expected, but this may differ for Windows and/or for different binary code.

Re: apache caches php random numbers?

PostPosted: 08. September 2016 18:05
by gsmith
Works on my Windows server.

We are going to flip a coin until we get three heads in a row!
It took 36715 flips!
It took 459 flips!
It took 24909 flips!
It took 1821 flips!
It took 916 flips!

Re: apache caches php random numbers?

PostPosted: 08. September 2016 19:11
by Nobbie
gsmith wrote:We are going to flip a coin until we get three heads in a row!
It took 36715 flips!
It took 459 flips!
It took 24909 flips!
It took 1821 flips!
It took 916 flips!


But this is only "the first try". Now close the browser, probably shut down Apache, restart Apache, start your Browser and then try again. As far as I understood, then the number of flips behave in the same way as in your example. I also took a while to understand this issue, the question does not imply that everytime the number of flips is the same, but it seems that the sequence of occurences is repeating. I cannot reproduce that behaviour, but it may also differ for PHP as module or PHP as CGI standalone linked executable. I dont know.

Anyway, using srand() with a unique number (timestamp or so) in the beginning *should* lead to really random behaviour. Unfortunately we get no feedback from mishamax.