apache caches php random numbers?

Problems with the Windows version of XAMPP, questions, comments, and anything related.

apache caches php random numbers?

Postby mishamax » 02. September 2016 13:20

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!
mishamax
 
Posts: 1
Joined: 02. September 2016 13:04
XAMPP version: 3.2.2
Operating System: windows 7

Re: apache caches php random numbers?

Postby JJ_Tagy » 02. September 2016 18:38

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.
JJ_Tagy
 
Posts: 788
Joined: 30. January 2012 13:44
XAMPP version: 5.5.15
Operating System: Windows 10 Pro x64

Re: apache caches php random numbers?

Postby Nobbie » 03. September 2016 12:46

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.
Nobbie
 
Posts: 13176
Joined: 09. March 2008 13:04

Re: apache caches php random numbers?

Postby gsmith » 08. September 2016 18:05

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!
gsmith
 
Posts: 278
Joined: 29. November 2013 18:04
Location: San Diego
XAMPP version: 0.0.0
Operating System: Win 10/2012R VS 14,15,16

Re: apache caches php random numbers?

Postby Nobbie » 08. September 2016 19:11

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.
Nobbie
 
Posts: 13176
Joined: 09. March 2008 13:04


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 170 guests