Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Randomizing "statically" on a daily/weekly basis

by hacker (Priest)
on Mar 11, 2005 at 02:08 UTC ( [id://438496]=perlquestion: print w/replies, xml ) Need Help??

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

I have a need to randomize a header image on a CGI on a slightly-longer interval than I am doing right now (currently every page load). I can randomize the image on every page load, but I'd like it to remain static for a longer period of time.

What's the best way to do this, without writing any lock files or without adding a row in a database, if possible? If not, what's the best way to do this with a drop file?

Or is there another approach aside from using 'my $random = int(rand(n));' on a periodic basis or every page load?

Replies are listed 'Best First'.
Re: Randomizing "statically" on a daily/weekly basis
by Zaxo (Archbishop) on Mar 11, 2005 at 02:18 UTC

    Set up a cron job to select an image and store its uri in a template or config file. You can pick any interval you like with that and it adds little overhead to the system.

    After Compline,
    Zaxo

Re: Randomizing "statically" on a daily/weekly basis
by japhy (Canon) on Mar 11, 2005 at 02:23 UTC
    Heh, a friend of mine just did something similar. He has a "quote of the minute" on his web site.

    The concept is thus:

    my @images = (...); # new one each minute my $display = $images[(time/60) % @images];
    Is that sufficient? Divide by 3600 for an image each hour, etc.
    _____________________________________________________
    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re: Randomizing "statically" on a daily/weekly basis
by jdporter (Paladin) on Mar 11, 2005 at 02:22 UTC
    Not sure how you're currently randomizing; but if you're using rand(), you could try seeding the RNG with just the date part of the current epoch time; or, e.g. take the epoch time (returned from time(), that is), shift it right by 11 bits, and seed the RNG with that. This will cause rand() to return the same value all day (or whatever the interval is).
Re: Randomizing "statically" on a daily/weekly basis
by Thilosophy (Curate) on Mar 11, 2005 at 02:23 UTC
    I think you want a timed rotation:
    my $interval = 60*60; # one hour my $random = int(time/$interval)%$n; # not random, though
    This will make a new number between 0 .. n every hour. Totally predictable, but that is probably okay in this case.
Re: Randomizing "statically" on a daily/weekly basis
by jhourcle (Prior) on Mar 11, 2005 at 02:26 UTC

    The 'best' way depends on what resources you have to deal with. Personally, I'd just set a cron job to randomly select the image each morning, and move it from whatever holding spot I had to where the general public would see it.

    If you only have CGI, and no cron, then I'd just use a randomizing function that took the time as a seed, and was deterministic. For instance, the modulus of number of days since Jan 1, 2000 and the number of images I had to rotate, if it were to change on a daily basis. (no, it's not completely random...it's just an example ... you can use whatever function you want, so long as it returns the same value for each seed.) If you want it more random, just use rand() and Memoize.

Re: Randomizing "statically" on a daily/weekly basis
by betterworld (Curate) on Mar 11, 2005 at 02:31 UTC
    Write a daemon (or cronjob) that randomizes the image once per hour or something. Let's assume that your CGI script looks for the image in the file foo.jpeg. Then your daemon would write the new image to foo.jpeg.tmp, and when it's finished, it issues rename("foo.jpeg.tmp", "foo.jpeg"), overwriting the old image. If you use Unix, this works without locking, because the rename(2) call is atomic and there is no time where a CGI program would find a half-ready image or something like this.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://438496]
Approved by blokhead
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (7)
As of 2024-04-19 08:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found