Page 1 of 1

one random number per day

Posted: Sat Jan 09, 2010 2:03 pm
by abushahin
Hey, I just wanted to ask if anybody can show me how i can create a random number that doesnt change for 24hours, I know i could use rand() but that means it changes every refresh, I could also use a cookie and set it to expire in 24hours but is there a way to do it just using php? I need it for my site where the user sees the same message for a day and a new one next day.

Re: one random number per day

Posted: Sat Jan 09, 2010 3:53 pm
by Apollo

Code: Select all

md5('RaNd0m~'.date('D d F Y'))

Re: one random number per day

Posted: Sat Jan 09, 2010 4:05 pm
by omniuni
You could also seed the random number generator with the date:

http://www.php.net/manual/en/function.mt-srand.php

Re: one random number per day

Posted: Sat Jan 09, 2010 5:04 pm
by abushahin
Apollo wrote:

Code: Select all

md5('RaNd0m~'.date('D d F Y'))
Hey thanks for your reply, I dont need a hash generated, neway the code u put up I changed to

Code: Select all

rand(1,100).date('D d F Y');
but this generates a random number as the nature of rand(), I wanna know how can I keep it consistent for at least 24 hours.

Re: one random number per day

Posted: Sat Jan 09, 2010 10:22 pm
by Apollo
You could use crc32 instead of md5 if you need it to be a number, but as suggested by omniuni you can also seed the random generator with the current day.

Code: Select all

mt_srand(crc32(date('D d F Y')));
print(mt_rand(1,100));

Re: one random number per day

Posted: Sat Jan 09, 2010 10:50 pm
by SimpleManWeb
You could do it with some simple mySQL calls. Create a really basic table that has two columns. Date and Number. Then, just write a little bit of code at the opening of your page that checks to see if the current date does not equal the date saved in the database and if the dates don't match you generate a new number, save it to the database and use it for whatever else you need it for.

Let me know if this makes sense. I could type up some quick code if you need it.

Re: one random number per day

Posted: Sun Jan 10, 2010 4:44 am
by abushahin
Hey, I could do that, check against db to check if its equal to date, ok but my question is how will I get it to automate one random per day, I could run the script manually once everyday that creates a rand() but I'd prefer if it did it automatically any suggestions?

Re: one random number per day

Posted: Sun Jan 10, 2010 9:54 am
by SimpleManWeb
Yeah, that should be pretty easy. Have the script check the date in the database every time someone visits the website. Then, have it only generate a new random number if the date doesn't match today. Don't use mySQL to create the new random number, use PHP. Here's some example code:

Code: Select all

 
    include "db_connnect.php";
    $Select = "Select `Date`,`Number` from `RandomNumber` limit 1"; //There should only ever be one record in this table anyways
    $Result = mysql_query($Select);
    $Array = mysql_fetch_array($Result);
    if ($Array['Date'] != date("Y-m-d")) {
         //Then it must be the next day
         $Number = 0;
         while($Number == $Array['Number'] || $Number == 0) { //Keep looping through until we really get a new number
             $Number = rand(0,10); //0 is Minimum, 10 is Maximum. Adjust as necessary;
         }
         //We have a new number now, so just save it to the database and we are done
         $SQL = "Update `RandomNumber` set `Date` = '".date("Y-m-d")."', `Number` = '".$Number."'";
         $Result = mysql_query($SQL);
    } else {
        $Number = $Array['Number'];
    }
    //OK, we now have our random number `$Number` and the database is up to date
 
I didn't test this code but it should work for you. Just stick it at the very top of your page. The only thing you will need to do is create the table with those two fields and seed it with your first "Random" number.

Hope this helps

Re: one random number per day

Posted: Sun Jan 10, 2010 10:07 am
by Apollo
abushahin wrote:Hey, I could do that, check against db to check if its equal to date, ok but my question is how will I get it to automate one random per day, I could run the script manually once everyday that creates a rand() but I'd prefer if it did it automatically any suggestions?
Ehh, my code above does exactly this. It gives you a unique random number (between 1 and 100) depending on the date.

Re: one random number per day

Posted: Sun Jan 10, 2010 10:33 am
by abushahin
Apollo wrote:You could use crc32 instead of md5 if you need it to be a number, but as suggested by omniuni you can also seed the random generator with the current day.

Code: Select all

mt_srand(crc32(date('D d F Y')));
print(mt_rand(1,100));
Hey appollo, yes ur code does just that, i wondered is there a need for both D and d to be present in the date?
And thanks to the rest! simpleman for the effort, i may use either depending on which is the simplest, simpleman one is most effective as this will allow to keep a history also if i ammended it, thanks guys

Re: one random number per day

Posted: Sun Jan 10, 2010 10:37 am
by AbraCadaver
Should work on a per user basis if that's what you want:

Code: Select all

session_start();
 
if(!isset($_SESSION['time']) || ($_SESSION['time'] + 86400) < time()) {
    $_SESSION['time'] = time();
    $_SESSION['rand'] = mt_rand(1,100);
}
$rand = $_SESSION['rand'];