def maybe_random_prime_from_range(self, a, b):
    if b < a:
        return None
while self.candidate < b:
    self.process_candidate()
ai = bisect.bisect(self.primes, a)
bi = bisect.bisect(self.primes[ai:], b)
try:
    return random.choice(self.primes[ai:ai + bi])
except:
    return None
ps = Primes()
print(ps.maybe_random_prime_from_range(4000, 4200))
Creating only one random prime number in provided range