Forum Moderators: coopster

Message Too Old, No Replies

Random Number Every Week

random number every week

         

cookiemonster

9:59 pm on May 2, 2010 (gmt 0)

10+ Year Member



Hi,

Is there any way I can make a PHP code that echoes a different random number once a week?

Thanks in advance.

eelixduppy

10:03 pm on May 2, 2010 (gmt 0)



Create a seed that only changes once per week (e.g. by using the week number 1-52) and then feed it to srand(). This will give you the same set of random numbers for the whole week, but will change at the beginning of a new week.

cookiemonster

10:50 pm on May 2, 2010 (gmt 0)

10+ Year Member



Thank you eelixduppy, that sounds promising.

Do you think you could supply a sample code? I am fairly new to PHP and have only a vague idea of what you are suggesting.

Thanks again!

Readie

12:34 pm on May 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think this'll work... I'm not waiting around for a week to test it though :P

srand(floor(date('U')) / (60*60*24*7));
echo rand() % 100;

Doesn't quite follow eelix's idea, as I think that basing it by week number will actually loop year-on-year.

This is based on seconds since the unix epoch, so it should hopefully not be predictable at all without actually knowing the code.

cookiemonster

8:33 pm on May 3, 2010 (gmt 0)

10+ Year Member



Readie,

Thanks for the response ... what exactly is the '% 100' used for in this script?
I'll check the script again in one week to see if it works ... unless there is some way to modify it to work every hour for testing purposes?

Thanks again.

cookiemonster

8:40 pm on May 3, 2010 (gmt 0)

10+ Year Member



Oh, I realize now that % 100 is, for example, the random number between 0 and 100 (%500 would be between 0 and 500).
Can the 100 somehow be substituted for a variable? For example,
echo rand() % $stuff
?

Readie

9:20 pm on May 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



% 100 returns the remainder after having divided by 100.

You could replace it with a variable however, yes.

cookiemonster

12:22 pm on May 6, 2010 (gmt 0)

10+ Year Member



Readie, your solution was working wonderfully up until now.
After two days, the number changed!

Here is my EXACT code -


<?php
srand(floor(date('U')) / (60*60*24*7));
$num = 100;
$a = rand() % $num;
echo $a;
echo "<br>";
$b = rand() % $num;
echo $b;
echo "<br>";
$c = rand() % $num;
echo $c;
echo "<br>";
$d = rand() % $num;
echo $d;
echo "<br>";
$e = rand() % $num;
echo $e;
?>

Readie

12:49 pm on May 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well you wanted a randum number every week, see if the number changes again in another seven days before thinking it's broken :)

If you want to set an off-set for this by the way (say, you want it to change on a Monday rather than a Thursday, or at a different hour, minute or even second) then this code should allow you to fine-tune it a bit:

$day_offset = 0;
$hour_offset = 0;
$minute_offset = 0;
$second_offset = 0;

srand(floor(date('U') / ((60*60*24*7) + (((60*60*24) * $day_offset) + ((60*60) * $hour_offset) + (60 * $minute_offset) + $second_offset));
echo rand() % 100;


Change those offset variables to +/- when the number changes.

Readie

5:13 pm on May 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Man what was I thinking when I typed that...

$day_offset = 0;
$hour_offset = 0;
$minute_offset = 0;
$second_offset = 0;

srand(floor((date('U') + (60 * 60 * 24 * $day_offset) + (60 * 60 * $hour_offset) + (60 * $minute_offset) + $second_offset) / (60 * 60 * 24 * 7));
echo rand() % 100;

Is what you really want.

cookiemonster

1:54 am on May 14, 2010 (gmt 0)

10+ Year Member



Yeppers ... you were right Readie - apparently it changes on Thursdays.
Works PERFECTLY now!

Thanks again!