brel-logo-white brel-logo-white
  • Travel
  • Case Study
  • How to
  • Technology
  • More
    • Sports
    • Health
    • Articles
    • Review
Notification
  • HomeHome
  • Finance
  • Health
  • Lifestyle
  • Sports
  • Contact Us
BreldigitalBreldigital
  • HomeHome
  • Finance
  • Health
  • Lifestyle
  • Sports
  • Contact Us
Search
  • Quick Access
    • Home
    • History
    • My Saves
    • My Interests
    • My Feed
  • Categories
    • Travel
    • Sports
    • Health

Top Stories

Explore the latest updated news!

Stephanie puts 30 cubes in a box

Which statement correctly describes the process that occurs in the thylakoid?

Which point is on the line that passes through point h and is perpendicular to line fg?

Stay Connected

Find us on socials
248.1k Followers Like
61.1k Followers Follow
165k Subscribers Subscribe
Breldigital > Blog > ds > Python generate list of random numbers
ds

Python generate list of random numbers

By John Published November 10, 2022
Share

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])

TAGGED: Python generate list of random numbers
John November 10, 2022 November 10, 2022

Search

brel-logo-white brel-logo-white
Explore a wide range of brands and categories with our comprehensive coverage, and stay up-to-date with the latest news and trends by subscribing to our updates.
Categories:
  • Entertainment
  • Travel
  • Sport
  • Contact Us

Quick Links

  • My Feed
  • My Interests
  • History
  • My Saves

About US

  • Adverts
  • Our Jobs
  • Term of Use

© 2023 Brel Digital All Rights Reserved. All logos and images used on this website are registered trademarks of their respective companies

Welcome Back!

Sign in to your account

Lost your password?