Can Python generate a random number that excludes a set of numbers, without using recursion?
python, random, range, recursion
Solution
Use random.choice(). In this example, a is your lower bound, the range between b and c is skipped and d is your upper bound.
import random
numbers = range(a,b) + range(c,d)
r = random.choice(numbers)
Problem
I looked over Python Docs (I may have misunderstood), but I didn't see that there was a way to do this (look below) without calling a recursive function. What I'd like to do is generate a random value which excludes values in the middle. In other words, Let's imagine I wanted `X` to be a random number that's not in `range(a - b, a + b)` Can I do this on the first pass, or 1. Do I have to constantly generate a number, 2. Check if in `range()`, 3. Wash rinse ? As for why I don't wish to write a recursive function, 1. it 'feels like' I should not have to 2. the set of numbers I'm doing this for could actually end up being quite large, and ... I hear stack overflows are bad, and I might just be being overly cautious in doing this. I'm sure that there's a nice, Pythonic, non-recursive way to do it.