Pick a number 1 through 20

By John

This is a Python Program to generate random numbers from 1 to 20 and append them to the list.

Problem Description
The program takes in the number of elements and generates random numbers from 1 to 20 and appends them to the list.

Problem Solution
1. Import the random module into the program.
2. Take the number of elements from the user.
3. Use a for loop, random.randint() is used to generate random numbers which are them appending to a list.
4. Then print the randomised list.
4. Exit.

Program/Source Code
Here is source code of the Python Program to generate random numbers from 1 to 20 and append them to the list. The program output is also shown below.

import random
a=[]
n=int(input(“Enter number of elements:”))
for j in range(n):
a.append(random.randint(1,20))
print(‘Randomised list is: ‘,a)

Runtime Test Cases

Case 1:
Enter number of elements:3
(‘Randomised list is: ‘, [17, 4, 9])

Case 2:
Enter number of elements:7
(‘Randomised list is: ‘, [16, 5, 18, 19, 6, 7, 20])