>>> population = range(10)
Are you allowed to use random.sample
?
>>> names = random.sample(population, 5)
>>> names
[4, 0, 1, 2, 8]
Using solely random.randint
would be more difficult, but if you must do so, I'd do something like this:
>>> names = [population[random.randint(0, len(population)-1)] for x in range(5)]
>>> names
[2, 8, 6, 6, 9]
But obviously this is with replacement. If you don't want replacement, you'd need some more complicated code. For example, a function:
def custom_sample(population, count):
names = []
already_used = []
for x in range(count):
temp_index = random.randint(0, len(population)-1)
while temp_index in already_used:
temp_index = random.randint(0, len(population)-1)
names.append(population[temp_index])
already_used.append(temp_index)
return names
So:
>>> names = custom_sample(population, 5)
>>> names
[7, 4, 8, 3, 0]