Jump to content


Photo

Random Number Every 24 Hours


  • Please log in to reply
8 replies to this topic

#1 Singularity

Singularity
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 25 August 2006 - 02:31 AM

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.



#2 jvalarta

jvalarta
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 25 August 2006 - 02:46 AM

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"

#3 Singularity

Singularity
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 25 August 2006 - 02:51 AM

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.

#4 jvalarta

jvalarta
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 25 August 2006 - 03:06 AM

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.

#5 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 25 August 2006 - 03:17 AM

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.
Legend has it that reading the manual never killed anyone.
My site

#6 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 25 August 2006 - 03:21 AM

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..
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#7 Singularity

Singularity
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 25 August 2006 - 03:40 AM

i understand that i must use a flatfile db. can someone please post the method to do iT w/o cron? or if any other solutions are out there, please inform.

#8 Ifa

Ifa
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationRauma, Finland

Posted 25 August 2006 - 03:47 AM

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 :)
I'm just a guy who likes to code for fun...

#9 Singularity

Singularity
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 25 August 2006 - 11:43 AM

Ok, what exactly is the command that I have to run in the Cron to call up the numberfile.txt located in the public_html folder?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users