Are there any best practices for handling the exclusion of a specific number when using rand() in PHP?

When using rand() in PHP to generate random numbers within a specific range, there is no built-in functionality to exclude a specific number from being generated. One way to handle this is to generate a random number within the desired range, check if it is the excluded number, and if so, generate a new random number until a different one is obtained.

function rand_exclude($min, $max, $exclude) {
    do {
        $randNum = rand($min, $max);
    } while ($randNum == $exclude);

    return $randNum;
}

// Example usage
$excludedNum = 5;
$randomNum = rand_exclude(1, 10, $excludedNum);
echo $randomNum;