Directly generates a list of random numbers using sample method

Python Python Module (Article) Python Module (Program)

142

The random module also provides the sample() method, which directly generates a list of random numbers. Below is the example of generating random numbers using the sample() method.

Program:

import random  
#Generate 5 random numbers between 10 and 30  
random_list = random.sample(range(10, 40), 6)  
print(random_list)  

Output:

[18, 25, 26, 29, 14, 31]

Explanation:

In the above code, we have used the range() function, which generates the numbers between the given range.


This Particular section is dedicated to Programs only. If you want learn more about Python. Then you can visit below links to get more depth on this subject.