Question

I have multiple image file names in an array and would like to generate random image that are fixed for a whole day, so I would like to use a fixed seed for the same day using date("Ymd") and this seed will change daily, thus generating new unique seed every day.

Problem is that array_rand doesn't accept seed as a parameter.

Was it helpful?

Solution 2

Having the same seed will not guarantee that you will get the same number every time. I would create some pseudo-random function, for example dependent from year, month and day, but not hours, minutes, seconds, that will give the same result only from these values, for example

function pseudorandom_day_image_index($array_of_pictures){
   $year=date('Y');
   $month=date('m');
   $day=date('d');
   return ($year*$month*$day) % count($array_of_pictures);
}

This will give you constant (for a day) index of a picture

OTHER TIPS

compute the hash of the human readable string returned by date("Ymd"), you'll get an unique seed for every day

something like:

$seed = md5(date("Ymd"));

Init yout rand function with your day-seed:

srand((int)$seed);

your new array index will be:

$random_array_index = $rand() % $array_size;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top