Generate random numbers from an interval with holes
0 1 2 3 4 5 6 7 8 9

0 1 2 3 - 5 - 7 8 -


"-" are in $S = \{4,6,9\}$

You can create mass distribution with  $$\text{Prob}(x) = \begin{cases}\frac{1}{N - |S|} &amp; \text{if } x \notin S\\
0 &amp; \text{if} \ x \in S
\end{cases}
 $$

and then use this algorithm. Copied from here.

from collections import defaultdict
import random

def roll(massDist):
    randRoll = random.random() * sum(massDist) # in [0,1)
    s = 0
    result = 0
    for mass in massDist:
        s += mass
        if randRoll &lt; s:
            return result
        result+=1

sampleMassDist = [1,1,1,1,0,1,0,1,1,0]

d = defaultdict(int)
for i in range(1000):
  d[roll(sampleMassDist)] += 1

print d