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?
How to get a random value from 1~N but excluding several specific values in PHP?
How to get a random number from a range, excluding some values
PS- I have already tried googling.Even Bing