Directly generates a list of random numbers using sample method
In this program you will how to generates a list of random numbers directly using sample method without a loop
In this program you will how to generates a list of random numbers directly using sample method without a loop
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.
import random
#Generate 5 random numbers between 10 and 30
random_list = random.sample(range(10, 40), 6)
print(random_list)
[18, 25, 26, 29, 14, 31]
In the above code, we have used the range() function, which generates the numbers between the given range.
First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.
Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.