Site icon T4Tutorials.com

Random Numbers in Python

Write a Program in Python to find Random Numbers.

Random Number Using for loop

The randint() function with loop can be used to produce a list of random numbers.

Step 1: Create an empty list

Step 2: Append the random numbers produced to the empty list one by one.

import random  
rand_list = []  
for i in range(0,20):  
    n = random.randint(1,50)  
    rand_list.append(n)  
print(rand_list)

Output

[37, 48, 33, 22, 35, 29, 11, 1, 37, 3, 7, 1, 21, 49, 5, 9, 49, 38, 29, 19]

import random  
rand_list = []  
for i in range(30,33):  
    n = random.randint(1,50)  
    rand_list.append(n)  
print(rand_list)

Output

[21, 6, 39]

 

 

Exit mobile version