Paulp51d Posted June 5, 2015 Share Posted June 5, 2015 Hi, I am writing some code as part of a learning exercise. Ultimately, I want to continuously generate random numbers between 0 and 300 and display this as a single, changing random number until a particular future date/time has been reached. So, given that I am a total newbie, I have begun by experimenting with the while loop, but with unpredictable results. I have commented the following code so that you can see what I expect it to do - which is basically keep generating random numbers forever, as the future date is a long way off. It appears to work sometimes, other times it just freezes everything up. I also tried placing the sleep(5); in the while statement to see if that would delay the display of the random numbers, but that always caused it to fail. Can anybody offer any expert advice. Thanks Paul <?php // subtract timestamp for the present time from the timestamp of some future date and store it in $values1. $values1 = mktime(16,0,0,6,14,2015)-time(); while ($values1 > 0) { #Has the future date been reached? echo rand(0,300). "<br/>"; #No, so generate and display a random number? $values2 = mktime(16,0,0,6,14,2015); #update the value of $values1, ready for retest in the while loop $values3 = time(); # I realise that I could have just done $values1 = mktime(16,0,0,6,14,2015)-time() again. $values1 = $values2 - $values3; # but it doesn't work either way } ?> Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted June 5, 2015 Share Posted June 5, 2015 web servers are not designed to work this way. they are designed to accept a http request from a client (browser) and output the requested page. to do what you are asking would require that the server side code test if the current time is less than the designated end time and just output a random number if it is. it would be up to the client/browser to periodically make http requests to the server to get and display the random number. Quote Link to comment Share on other sites More sharing options...
requinix Posted June 5, 2015 Share Posted June 5, 2015 That's a pretty complicated way of doing a loop until a time has passed. $future = mktime(16, 0, 0, 6, 14, 2015); while (time() < $future) { echo rand(0, 300); }That will generate a lot of numbers because the loop will execute very quickly. So this approach doesn't make sense to me. What are you actually trying to accomplish? Quote Link to comment Share on other sites More sharing options...
Paulp51d Posted June 5, 2015 Author Share Posted June 5, 2015 HI guys. Thanks for the answers, which I really appreciate. Mostly I am doing this as a learning exercise for, but with a live website in mind as well. I had a competition in mind, where the entrants would leave their name and address and be assigned a number. If I limited the competition to 300 entrants, then each person would acquire a number from 1-300, sequentially. If I then said that the randomly selected winner would be drawn on 1st July, 2015 say, I was thinking that the page could flash up a random number between 1 and 300, every 10 seconds, say, until the draw date. I could then have a countdown timer on the page and when the clock stopped the last randomly generated number wins. The purpose of having the randomly generated number appearing on the screen every few seconds would simply be to illustrate that the competition was honest and above board. So, you think that this is not really do-able or sensible? Paul Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted June 5, 2015 Share Posted June 5, 2015 Showing constant random numbers 1-300 every user for pretty much no actual purpose doesn't sound like a good idea. But if were to do so I would probably use javascript to do it. The purpose of having the randomly generated number appearing on the screen every few seconds would simply be to illustrate that the competition was honest and above board. Doesn't quite prove it's honest, you can still assign any number you desire as output. 1 Quote Link to comment Share on other sites More sharing options...
fastsol Posted June 5, 2015 Share Posted June 5, 2015 Displaying the numbers before the actual end time would only confuse the clients viewing it beforehand, especially since the shown numbers have no content value to them what so ever. As stated, it doesn't prove any kind of trust. Quote Link to comment Share on other sites More sharing options...
Paulp51d Posted June 5, 2015 Author Share Posted June 5, 2015 Well thanks all, I really appreciate the help. Of course I understand that the randomly generated numbers would not actually really prove anything, but It would look and feel like an authentic procedure. And don't forget I was wanting to explore what could be done with PHP - which you have helped me to do. Thanks. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.