Zendie
Zendie

Reputation: 1174

random number generate in 5 minutes

How can I generate a random number in every 5 minutes? When I searched for it, I found Need to generate random number after time interval. Using that, I coded like,

    <?
    $seed = floor(time()/(60*60*12));
    srand($seed);
    $item = rand(0,9);
    echo $item;
    ?>

But the value of $item is not changing in 5 minute. How can I edit this code?

Upvotes: 2

Views: 3192

Answers (2)

Sean Johnson
Sean Johnson

Reputation: 5607

The example you pulled from the other SO question generates a new random number every TWELVE HOURS, as indicated by the "12" in your code.

Since you want a new random number every FIVE MINUTES, the code is as follows:

<?
$seed = floor(time()/(60*5));
srand($seed);
$item = rand(0,9);
echo $item;
?>

Upvotes: 4

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798606

You seed it with the same value for every 12 hour period, which causes it to show the same result. Stop seeding unnecessarily.

Upvotes: 1

Related Questions