I have the code :
$length = 16;
$characters = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i ) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
$key = $randomString;
How to solve this if the string will be shuffled after 10 minutes, and the string is always the same every 10 minutes?
CodePudding user response:
If you want a random number generator to generate the same random sequence of numbers ever time it runs, then you need to seed the random generator with the same value. So if you want the same set of numbers for ten-minutes intervals, then you need to find a way to create a seed value that stays constant over ten minutes, and then changes to something else.
10 minutes is 600 seconds, so if you divide the current timestmap by 600, and take only the integer portion of the result - then you got a seed value that changes every ten minutes.
$seed = floor(strtotime('2021-01-01 12:00:00')/600);
srand($seed);
$length = 16;
$characters = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i ) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
echo $randomString;
Try that code above, and then change the time portion of the date value. Make it 12:03:00
first - should still get you the same random string. Make it 12:09:59
, still the same string. Make it 12:10:00
- different string!
So to have this code generate a new random string every ten minutes, just use it with the current timestamp,
$seed = floor(time()/600);