The randint() method can be used with for loop to generated a list of random numbers. To do so, we need to create an empty list, and then append the random numbers generated to the empty list one by one. Let's understand the following example.
import random
rand_list = []
for i in range(0,10):
n = random.randint(1,50)
rand_list.append(n)
print(rand_list)
[10, 49, 16, 31, 45, 21, 19, 32, 30, 16]
Using randint() method with for loop you can generate a list of random numbers.
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.