How to generate a random list of ints in Python3?

Question:

My idea is as follows:

l=[]
for i in range(10):
    l.append(random.randint(0,100))

But is there a more convenient way to generate a random list of ints since I’ve imported the random module?

Asked By: skyline

||

Answers:

my_list = [random.randint(0,100) for r in range(10)] 
Answered By: qristjan

Not sure what is most “convenient”, but random.choices returns a list of specified length from a given population.

random.choices(range(100), k=10)
Answered By: benvc
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.