one random number per day
Moderator: General Moderators
one random number per day
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
Code: Select all
md5('RaNd0m~'.date('D d F Y'))Re: one random number per day
You could also seed the random number generator with the date:
http://www.php.net/manual/en/function.mt-srand.php
http://www.php.net/manual/en/function.mt-srand.php
Re: one random number per day
Hey thanks for your reply, I dont need a hash generated, neway the code u put up I changed toApollo wrote:Code: Select all
md5('RaNd0m~'.date('D d F Y'))
Code: Select all
rand(1,100).date('D d F Y');Re: one random number per day
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));- SimpleManWeb
- Forum Commoner
- Posts: 57
- Joined: Wed Dec 30, 2009 4:15 pm
- Location: New Hampshire, USA
Re: one random number per day
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.
Let me know if this makes sense. I could type up some quick code if you need it.
Re: one random number per day
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?
- SimpleManWeb
- Forum Commoner
- Posts: 57
- Joined: Wed Dec 30, 2009 4:15 pm
- Location: New Hampshire, USA
Re: one random number per day
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:
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
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
Hope this helps
Re: one random number per day
Ehh, my code above does exactly this. It gives you a unique random number (between 1 and 100) depending on the date.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?
Re: one random number per day
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?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));
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
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: one random number per day
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'];mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.