How to generate a random list of fixed length of values from given range?
import random
def simplest(list_length):
core_items = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
random.shuffle(core_items)
return core_items[0:list_length]
def full_random(list_length):
core_items = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
result = []
for i in range(list_length):
result.append(random.choice(core_items))
return result
A random sample like this returns list of unique items of sequence. Don't confuse this with random integers in the range.
>>> import random
>>> random.sample(range(30), 4)
[3, 1, 21, 19]
A combination of random.randrange and list comprehension would work.
import random
[random.randrange(1, 10) for _ in range(0, 4)]