Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Can someone explain the methodology behind creating a random-per-day script? I've created countless scripts which produced random results each and every time the script was loaded (banner ads for example), but how would you go about doing it so it's the same data being processed for the entire day?

Not really looking for your codes to do this, just interested how you would/could do this. I'm not too familiar with storing in MySQL or anything like that, if that makes a difference.

Thank you wise monks!

Replies are listed 'Best First'.
Re: Controlled random scripts
by BrowserUk (Patriarch) on Jun 02, 2004 at 23:57 UTC

    Do whatever you are doing now to creeate/select your data on a per run basis using random but start the script with srand time % 86400;.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
Re: Controlled random scripts
by Abigail-II (Bishop) on Jun 03, 2004 at 14:06 UTC
    How often does this code run? If often, you just might want to generate values for specific days in advance and store them in a database. Even generating them for 100 years in advance costs only 36524 or 36525 rows, which is a pretty trivial amount. Just don't forget to run a cron job on each last day of a century. (Or you could just reuse them, noone will remember the banner ad they saw 100 years ago!)

    Aternatively, if it's for something like banner ads, or random images, just keep the location of the ad or image fixed. And replace the image or ad on a daily bases.

    Abigail

Re: Controlled random scripts
by eric256 (Parson) on Jun 02, 2004 at 23:01 UTC

    Simply pick a method of storing your choice. Then lookup the current date, if its not stored yet then randomly pick one and store it there. If you don't care if its random then you can just use the day of the month (or the day of the year, or the day of the week, or the month, etc) and do math with it that will always result in the same answer within your range.


    ___________
    Eric Hodges
Re: Controlled random scripts
by sulfericacid (Deacon) on Jun 03, 2004 at 04:27 UTC
    Simply create a hash for today's date. Every time the script loads, check to see if the date of the server is the same date in the hash. If it is, produce a certain result. If it isn't, delete the original date and use the new date. Then produce a different result for that day.


    "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

    sulfericacid
Re: Controlled random scripts
by thor (Priest) on Jun 02, 2004 at 23:42 UTC
    Basically, you need a function that returns the same output for a given input (in this case, your input would be the date). In that respect, it's not random at all. There are countless such functions: a simple sub that returns the date in YYYYMMDD format is one. Depending on how your data is indexed, you can probably come up with more useful functions than that.

    thor

Re: Controlled random scripts
by TomDLux (Vicar) on Jun 03, 2004 at 02:32 UTC

    When I worked for a porn web site, what they did for the sample front page images was to have a web page refer to an image, "pic1.jpg". Then once a day, a script would randomly select a picture from a gallery, and copy or link that to be accessed from "pic1.jpg".

    You could also save the generated value in a file or in a database. That might be more appropriate if it isn't simply one of a numbe of files, but something more sophisticated.

    --
    TTTATCGGTCGTTATATAGATGTTTGCA