Jump to content

random value based on timestamp


myphptrip

Recommended Posts

hi there, I'm using this function to get a random value from an array every new full hour.

function RandomQuoteByInterval($TimeBase, $QuotesArray){
 
    // Make sure it is a integer
    $TimeBase = intval($TimeBase);
 
    // How many items are in the array?
    $ItemCount = count($QuotesArray);
 
    // By using the modulus operator we get a pseudo
    // random index position that is between zero and the
    // maximal value (ItemCount)
    $RandomIndexPos = ($TimeBase % $ItemCount);
 
    // Now return the random array element
    return $QuotesArray[$RandomIndexPos];

it's initiated via something like this:


$DayOfTheYear = date('h'); 
 
// Example array with some random quotes
$RandomQuotes = array(
    'No animals were harmed in the making of this snippet.',
    'Nice snippets',
    'The modulus operator rocks!',
    'PHP is cool.'
);
 
print RandomQuoteByInterval($DayOfTheYear, $RandomQuotes);
 

Is my assumption correct that this will return the same value for let's say 3pm every day? because as far as I can read it, the randomness is defined only by the number of the hour.

 

If so, I am looking for a way that will change it. I don't want to return the same value that is returned on 3pm everyday. I want it to be random at the hour - but not constant across days.

 

I guess I could achieve it by adding day month and year value to the $RandomIndexPos?

$currentYear = date('Y'); 
$currentMonth = date('m'); 
$currentDay = date('d'); 

$RandomIndexPos = ($TimeBase % $ItemCount) + $currentYear +$currentMonth + $currentDay;

Would it work?

 

Link to comment
Share on other sites

Is my assumption correct that this will return the same value for let's say 3pm every day? because as far as I can read it, the randomness is defined only by the number of the hour.

Correct. And it's not random: it cycles through each quote in order... with a limit of 12 quotes because the 'h' value has a range of 1-12.

 

If so, I am looking for a way that will change it. I don't want to return the same value that is returned on 3pm everyday. I want it to be random at the hour - but not constant across days.

 

I guess I could achieve it by adding day month and year value to the $RandomIndexPos?

$currentYear = date('Y'); 
$currentMonth = date('m'); 
$currentDay = date('d'); 

$RandomIndexPos = ($TimeBase % $ItemCount) + $currentYear +$currentMonth + $currentDay;
Would it work?

 

Almost. You have to do the modulus (%) on the total number, not on just the $TimeBase.

$RandomIndexPos = ($TimeBase + $currentYear + $currentMonth + $currentDay) % $ItemCount;
However, you don't have to do it in RandomQuoteByInterval. In fact I would not. Just call the function with the new number.

 

And while I'm in the code, here's another way of getting the random result:

$TimeValue = floor(time() / 3600); // number of hours since Jan 1 1970
 
// Example array with some random quotes
$RandomQuotes = array(
    'No animals were harmed in the making of this snippet.',
    'Nice snippets',
    'The modulus operator rocks!',
    'PHP is cool.'
);
 
print RandomQuoteByInterval($TimeValue, $RandomQuotes);
As before this is not random: it will cycle through quotes in order. If you wanted it in "truly" random order then you'd need different code...
Link to comment
Share on other sites

 

As before this is not random: it will cycle through quotes in order. If you wanted it in "truly" random order then you'd need different code...

 

 

 

thank you!

$TimeValue = floor(time() / 3600); // number of hours since Jan 1 1970

makes more sense, yeah.

 

yeah it isn't that random - you can (obviously) calculate it.

since there are 50 items in the array I am actually using, it shouldn't matter I guess.

 

on the other hand: couldn't I just shuffle the array before itemcount kicks in?

Link to comment
Share on other sites

Not really: shuffle() is random so you'd end up with a different quote every time. You'd need something more than that. The standard way to deal with this is to use PHP's random number generators (rand(), mt_rand()) but seeding, the way you could get a set of stable random results, isn't reliable anymore: srand() is disabled if PHP is installed with the Suhosin patch/extension and that's popular with shared hosting environments, while mt_srand() won't guarantee the stability because of what it can do to the seed number.

 

A sort of pseudo-random shuffle would be fine: shuffle the items in the array so that quote #N doesn't always appear before quote #N+1. Something like

function RandomQuoteByInterval($TimeBase, $QuotesArray){
 
    // Make sure it is a integer
    $TimeBase = intval($TimeBase);
 
    // How many items are in the array?
    $ItemCount = count($QuotesArray);

    // Shuffle the array in a way that looks random
    // 1. Create an array of random strings for each quote
    $QuotesKeys = array();
    foreach ($QuotesArray as $N => $Quote) {
        $QuotesKeys[$N] = md5($TimeBase . $Quote);
    }
    // 2. Sort the keys array normally, and at the same time
    // rearrange the quotes to match
    array_multisort($QuotesKeys, $QuotesArray);
 
    // By using the modulus operator we get a pseudo
    // random index position that is between zero and the
    // maximal value (ItemCount)
    $RandomIndexPos = ($TimeBase % $ItemCount);
 
    // Now return the random array element
    return $QuotesArray[$RandomIndexPos];

}
The downside to using randomness is that a particular quote may show up twice (or more) in a row. There are ways to avoid that...
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.