How to Get 10 Random Numbers in Python

0 min read 84 words

The random module allows you to generate choices.

import random
print(random.choices([i for i in range(1000)], k=10))

This might give us back something like:

[635, 450, 26, 829, 786, 563, 294, 586, 893, 953]

Explaining random.choice and random.choices

random.choice takes a sequence as a parameter and return a single random item from it.

While random.choices takes a sequence as a parameter and returns a few random items from it. You can specify how many random items to return by populating the k= parameter, as above.

Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags