How can PHP code be optimized to efficiently handle the exclusion of specific numbers during random number generation for customer IDs?

When generating random customer IDs in PHP, one way to efficiently handle the exclusion of specific numbers is to generate a random number, check if it is in the excluded list, and if so, regenerate a new random number until a valid one is found. This ensures that the generated customer IDs do not contain the excluded numbers.

$excludedNumbers = [1001, 2005, 3003]; // List of numbers to exclude

function generateRandomCustomerID($excludedNumbers) {
    do {
        $randomID = mt_rand(1000, 9999); // Generate random number between 1000 and 9999
    } while (in_array($randomID, $excludedNumbers));

    return $randomID;
}

$customerID = generateRandomCustomerID($excludedNumbers);
echo $customerID; // Output a random customer ID excluding the specified numbers