How can I generate a random number within a range but exclude some, without keep generating and checking if the generated number is one of those that I want to exclude?
Depending on how large the list of random numbers you're excluding, I would just generate your numbers and check if it's in the array of excluded numbers- if it is, just discard it. I know you don't want to check every time, but I can't think of another way besides specifying the ranges explicitly and if you have more than 5 numbers you're excluding that might be a bit worse.
I think the additional question is: What are the numbers you want to exlcude? Do they represent some sort of range or are they totally random?
If it was a range of numbers you want to ignore, you could generate your random numbers from within few sets that represent only valid numbers:
rand(1,9);
rand(15,19);
rand(22,26);
this way you are sure you will never select excluded: <0,10,11,12,13,14,20,21,>27
Then, when you get your 3 numbers, you could again randomly select one of them.
If excluded numbers are all over the place than I'm afraid you'd have to check it every time against some sort of collection of excluded numbers.