Jump to content

Random Number Every 24 Hours


Singularity

Recommended Posts

Does anyone know how to make a script that generates a random number and stores it into a variable, but generates a random number for that variable every 24 hours? This is what I want to accomplish:

----
When ANY user visits my site on for example Monday, they see the number "5" (it is randomly generated out of 100 numbers). Any user who visits my site on Monday will see the number "5".

After 24 hours, the system will randomly choose another number out of 100, for example, 67, and every person who visits my site on Tuesday, will see the number 67.

After another 24 hours, the system will randomly choose another number out of 100, for example, 54, and every person who visits my site on Wednesday will see the number 54.
----

I hope you guys can help me out.

Link to comment
Share on other sites

Well, you could do something like this:

1) Create a cron to run every 24 hours to call a file that does this:

2)

$RandomNumber = rand(1,100); //PICK A NUMBER BETWEEN 1-100
$NumberFile = fopen("numberfile.txt", "w+"); //CREATE A FLATFILE TO STORE THE RANDOM NUMBER
fwrite($NumberFile, "$RandomNumber"); //WRITE THE NUMBER TO THE FILE
fclose($NumberFile); //CLOSE THE FILE

3) Have your script read the flat file "numberfile.txt"
Link to comment
Share on other sites

Im looking for another way to do it, preferrably a method that uses only PHP without having to use cronjobs. I heard someone say that you can use srand and microtime or something to do this, but I am not sure. Remember that the random number is stored in a variable. So like $therandomnumber is equal to for example, 64 one day, and then 30 the next day, 40 the day after, etc. As long as it keeps the same number for 24 hours, and then the number changes after 24 hours.

If someone can help me come up with a solution to this small problem using only PHP without the use of Cronjobs or database, please let me know. Much appreciated.
Link to comment
Share on other sites

If you are going to create a number that needs to last for 24 hours for any user who visits, you need to store that number somehwere -- a flat file or a database. You can't use a session, or cookie, as that's a per user variable. The example I gave you would pick a number and store it in a flatfile...then you can very easily just read into a web page.

And as for the cron, if you want to automate this, you really need to use a cron to have it kick off whatever process you end up choosing to create the number. I don't know of any other method that you could automate something like this. i suppose to could create a quasi-daemon in PHP by creating a loop and using sleep()...but this is hooky at best.

Good luck.
Link to comment
Share on other sites

The only alternative to using a cron to kick-start the process is basically to include some code on your index page (or every page if you want) - to read a text file.  If today's date does not match the date stored in a small text file then re-write that text file with a new date AND a new random number, otherwise just read the random number of the day from that text file.
Link to comment
Share on other sites

only alternative to a cronjob is to have your script for the webpage itself check the time and update the file/db based on the time, whenever any  user accesses the webpage.  But at best, this is extra code and time wasted, and will never be accurate, because it will be executed everytime someone(s) accesses the page, and unless someone happens to access it at exactly midnight or whatever, it will not update until the first person of the next day accesses it.  I guess in reality if there is nothing else involved in this-- that is, all you want is this random number to be displayed, and this number will not be time sensitive for some other script running at certain times, then this method might actually be okay..if you have a relatively small amount of users..
Link to comment
Share on other sites

You could just change the number at midnight (if time = midnight). It won't change it until the first person visits the site though, but then again, what's the need for a chage if there's no one to see it :)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.