randomly generate a number every X minutes (php)
-
Hi, How can i randomly generate a number every (for example) 20 minutes with php? I know how to generate a number, but i have no idea how to make it generate one every X minutes. Thanks in advance!
-
This may sound like an obvious answer but you just put your random number generator in a loop, and include whatever PHP command is required to sleep for 20 minutes each time around the loop.
-
This may sound like an obvious answer but you just put your random number generator in a loop, and include whatever PHP command is required to sleep for 20 minutes each time around the loop.
I suggest you take the initial idea and find a way to create another process to do the other tasks that need to be done...I other words, you'll perform the update the time and sleep for 20 mins on one process thread and on the other thread you'd perform the remaining tasks. Hope this helps.
Plecco Technologies, Inc. Web Design | Software Development | Internet Marketing
modified on Wednesday, January 27, 2010 10:00 AM
-
I suggest you take the initial idea and find a way to create another process to do the other tasks that need to be done...I other words, you'll perform the update the time and sleep for 20 mins on one process thread and on the other thread you'd perform the remaining tasks. Hope this helps.
Plecco Technologies, Inc. Web Design | Software Development | Internet Marketing
modified on Wednesday, January 27, 2010 10:00 AM
-
Sorry I just joined and am learning the format for forum post.
Plecco Technologies, Inc. Web Design | Software Development | Internet Marketing
modified on Wednesday, January 27, 2010 9:57 AM
-
Sorry I just joined and am learning the format for forum post.
Plecco Technologies, Inc. Web Design | Software Development | Internet Marketing
modified on Wednesday, January 27, 2010 9:57 AM
plecco wrote:
Sorry I just joined and am learning the format for forum post.
May I suggest you spend time in "read only" mode until you are more familiar with how the forums operate, before starting to answer queries. That is, unless you absolutely have the solution to a poster's question.
-
Hi, How can i randomly generate a number every (for example) 20 minutes with php? I know how to generate a number, but i have no idea how to make it generate one every X minutes. Thanks in advance!
I may be a tad late to the game here but if you are using Linux or Mac this should work fine for your needs. --Terry Valladon
$random_number = rand(); // I will let you worry about seeds and stuff, perhaps call your own make_random() function? print('Random number is '.$random_number."\n"); declare(ticks = 1); // Set ticks to one second pcntl_signal(SIGALRM, "signal_handler"); // Installs a signal handler for SIGALARM (works in linux/mac only I think) pcntl_alarm(1200); //trigger alarm in 20 min (60 seconds * 20 min = 1200 seconds) // This is just to keep the script running forever, I would HOPE you would have real code in here rather then just looping endlessly. for(;;) { sleep(1); } function signal_handler($signal) { global $random_number; switch($signal) { case SIGALRM: $random_number = rand(); // New random number, I will let you worry about seeds and stuff, perhaps call your own make_random() function? print('New random number is '.$random_number."\n"); pcntl_alarm(1200); //trigger alarm again in 20 min break; // Exit function and continue normal code execution. } }