skyline
skyline

Reputation: 473

How to generate a random list of ints in Python3?

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?

Upvotes: 0

Views: 60

Answers (2)

benvc
benvc

Reputation: 15120

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)

Upvotes: 1

qristjan
qristjan

Reputation: 183

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

Upvotes: 1

Related Questions