You could use the random.sample
function from the standard library to select k elements from a population:
import random
random.sample(range(low, high), n)
In case of a rather large range of possible numbers, you could use itertools.islice
with an infinite random generator:
import itertools
import random
def random_gen(low, high):
while True:
yield random.randrange(low, high)
gen = random_gen(1, 100)
items = list(itertools.islice(gen, 10)) # Take first 10 random elements
After the question update it is now clear that you need n distinct (unique) numbers.
import itertools
import random
def random_gen(low, high):
while True:
yield random.randrange(low, high)
gen = random_gen(1, 100)
items = set()
# Try to add elem to set until set length is less than 10
for x in itertools.takewhile(lambda x: len(items) < 10, gen):
items.add(x)