function generateRandomString(): string
{
$letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$digits = '0123456789';
$specials = '.~^&*()';
$allowed = $letters . $digits . $specials;
$length = random_int(8, 25);
$characters = [];
// Guarantee at least two characters from each category.
foreach ([$letters, $digits, $specials] as $pool) {
for ($i = 0; $i < 2; $i++) {
$characters[] = $pool[random_int(0, strlen($pool) - 1)];
}
}
// Fill the remaining positions with any allowed characters.
while (count($characters) < $length) {
$characters[] = $allowed[random_int(0, strlen($allowed) - 1)];
}
// Fisher-Yates shuffle so the required categories aren't in fixed positions.
for ($i = count($characters) - 1; $i > 0; $i--) {
$j = random_int(0, $i);
$temp = $characters[$i];
$characters[$i] = $characters[$j];
$characters[$j] = $temp;
}
return implode('', $characters);
}