+2 votes
in Programming Languages by (74.2k points)
edited by
I want to create a list of random numbers that are less than 100. What is the Pythonic way to do this?

1 Answer

+1 vote
by (350k points)
selected by
 
Best answer

You can use the sample() function of the random module of Python. It returns a k-length list of unique elements chosen from the population.

random.sample(population,k)

Here is an example:

>>> import random
>>> random.sample(range(100),20)
[39, 0, 83, 63, 34, 35, 4, 3, 93, 50, 55, 74, 10, 96, 11, 29, 23, 27, 2, 80]
>>> random.sample(range(100),20)
[46, 94, 3, 7, 60, 32, 72, 93, 86, 57, 80, 95, 34, 70, 45, 71, 51, 89, 77, 84]


...