Random static date generation?
March 6, 2010 11:32 AM   Subscribe

I need to create a static random number based solely on the date in PHP...I *think* this is possible, but I'm not sure.

I have a small system set up where I need to generate a single random number between two values (15 and 20, for this example) based on the date. So, for example, today, no matter how many times the function is called, it will always return, say, "16" because that's the random number based on today's date. But tomorrow, once the date switches over, it will be, say, "20" no matter how many times the function is called.

I've thought about populating a mysql table with random values to call from the database so they'll be constant based on the date, but don't want to generate a table of dates and numbers for every date from here out to whenever I might stop using the system.

The only way I can think of doing this in just php is to grab the current date and somehow base the formula off of that. I could use the rand() function, but every time I call the page, it will display a different random number between 15 and 20 for that particular day. I want the random number to be constant for each date.

Thanks, mefi!
posted by omnipotentq to Computers & Internet (7 answers total) 1 user marked this as a favorite
 
Best answer: That's not a random number. That's a hash. That's how you should be thinking about it.
posted by Chocolate Pickle at 11:51 AM on March 6, 2010 [3 favorites]


Can you generate a random number the first time the page is called, and store it in a file on the server?
posted by jozxyqk at 11:51 AM on March 6, 2010


Store the determined number the first time the page is called on the current date. In database terms: a single row table with a date column and a value column. Or just put it in a file on disk. When the page is called, determine if current date matches the stored date. If not; update the date and value as required.
posted by mnology at 12:01 PM on March 6, 2010


Best answer: Definitely you want a hash function. A good hash will generate a *random distribution* into the number of slots available. Depending on what you are trying to do there will be collisions, that is two different dates hitting the same value. Generating a smooth distribution for a small number of slots could be tricky. Try adding the digits plus a constant to hit the range you want, loop through a few years and see if it's close enough.
posted by sammyo at 12:17 PM on March 6, 2010


Best answer: PHP has a number of hashing functions like md5(), sha1(), and hash() (which allows you to specify many different kinds of hashing). Once you've hashed the date, you'll probably want to use modulus and abs() to get the number in the right range.

Your code will probably look something like this (assuming the range 15-20): $num = abs(md5($date)) % 6 + 15
posted by Axle at 12:53 PM on March 6, 2010


Response by poster: Thanks, everyone! Sounds like a hash is what I'm looking for!
posted by omnipotentq at 1:00 PM on March 6, 2010


If you care about making it unfeasible to guess how the date relates to its "random" number, you should salt that hash: generate one random string, store it in your database as the salt value, then always append it to $date before feeding the whole lot through md5().
posted by flabdablet at 8:57 PM on March 6, 2010


« Older Convection Oven Dinners   |   Old gadget, new laptop, no connection Newer »
This thread is closed to new comments.