How to Get 10 Random Numbers in Python

The random module allows you to generate choices.

import random print(random.choices([i for i in range(1000)], k=10))
Code language: Python (python)

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:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments