Not a new question but its specific to only c/c++ . 

```
For ex- range[1...10] exclude_set [2,5,7,9] 

```

Custom rand function should return remaining numbers with equal probability.

What i have tried is-

1) generate number by rand() %10 function .

2) check if values is present in exclude\_Set .if present call rand() again in loop 

Interviewer said its brute force approach .I am curious that is there any good algorithm present for c/c++ 

The question is already answered for Java/C#/PHP on SO

[How can I generate a random number within a range but exclude some?](https://stackoverflow.com/questions/6443176/how-can-i-generate-a-random-number-within-a-range-but-exclude-some)

[How to get a random value from 1~N but excluding several specific values in PHP?](https://stackoverflow.com/questions/2698265/how-to-get-a-random-value-from-1n-but-excluding-several-specific-values-in-php)

[How to get a random number from a range, excluding some values](https://stackoverflow.com/questions/18484577/how-to-get-a-random-number-from-a-range-excluding-some-values)

**PS**- I have already tried googling.Even Bing