# Create a list of random numbers python – Python Program to Generate Random Numbers from 1 to 20 and Append Them to the List

Lists in Python:

How to create a list of random numbers in python: Python’s built-in container types are List and Tuple. Objects of both classes can store various additional objects that can be accessed via index. Lists and tuples, like strings, are sequence data types. Objects of different types can be stored in a list or a tuple.

A list is an ordered collection of objects (of the same or distinct types) separated by commas and surrounded by square brackets.

Random Numbers in Python:

Create a list of random numbers python: The random module in Python defines a set of functions for generating and manipulating random integers. Random(), a pseudo-random number generator function that generates a random float number between 0.0 and 1.0, is used by functions in the random module. These functions are used in a variety of games, lotteries, and other applications that need the creation of random numbers.

Examples:

Example1:

Input:

given number of elements =30

Output:

The random number list which contains 30 elements =  [6, 18, 11, 5, 16, 12, 6, 20, 12, 1, 5, 20, 20, 18, 13, 12, 5, 7,
14, 9, 4, 5, 13, 18, 19, 19, 19, 20, 12, 2]

Example2:

Input:

Enter the total number of elements in the given list =25

Output:

The random number list which contains 25 elements = [1, 6, 18, 5, 3, 2, 7, 5, 6, 6, 1, 2, 4, 17, 1, 20, 6, 2, 6, 19, 6,
10, 19, 20, 3]

## Program to Generate Random Numbers from 1 to 20 and Append Them to the List

Generate random list of numbers python: We will create a program that generates random numbers from 1 to 20 and adds them to a empty list using append() function.

Drive into Python Programming Examples and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.

### 1)randint function

List of random numbers python: Python has a random module that can create random integers. We utilized the random function in combination with the randint function to produce random numbers.

Syntax:

randint(start, end)

randint takes two inputs: a starting and an ending point. Both must be integers, with the first number always being less than the second.

### 2)Generating n random numbers using randint function(Static Input)

Approach:

• Import the random module to the code.
• Take a empty list which stores the random numbers.
• Using static input, give the number of elements in the list.
• Loop from 1 to number of elements using for loop
• Generate the random number by passing parameters 1 and 20 to randint function and store it in a variable.
• Append this random number variable to the list.
• Print the random number list

Below is the implementation:

# importing random module
import random
# Take a empty list which stores the random numbers.
randomlist = []
# Using static input, give the number of elements in the list.
terms = 30
# Loop from 1 to number of elements using for loop
for k in range(terms):
# Generate the random number by passing parameters 1 and 20 to
# randint function and store it in a variable.
randNum = random.randint(1, 20)
# Append this random number variable to the list using append() function
randomlist.append(randNum)
# print the random number list
print('The random number list which contains',
terms, 'elements = ', randomlist)


Output:

The random number list which contains 30 elements =  [6, 18, 11, 5, 16, 12, 6, 20, 12, 1, 5, 20, 20, 18, 13, 12, 5, 7,
14, 9, 4, 5, 13, 18, 19, 19, 19, 20, 12, 2]

Explanation:

• The random module has been included to the code.
• Using static input, give the number of elements in the list.
• Random.randint() is used in a for loop to produce random numbers, which are then appended to a list.
• The inputs sent to the random.randint() function are used to set the range within which the randomised numbers should be printed.
• The randomised list is then printed.

### 3)Generating n random numbers using randint function(User Input)

Approach:

• Import the random module to the code.
• Take a empty list which stores the random numbers.
• Scan the number of elements using int(input()).
• Loop from 1 to number of elements using for loop
• Generate the random number by passing parameters 1 and 20 to randint function and store it in a variable.
• Append this random number variable to the list.
• Print the random number list

Below is the implementation:

# importing random module
import random
# Take a empty list which stores the random numbers.
randomlist = []
# Scan the number of elements using int(input()).
terms = int(input('Enter the total number of elements in the given list ='))
# Loop from 1 to number of elements using for loop
for k in range(terms):
# Generate the random number by passing parameters 1 and 20 to
# randint function and store it in a variable.
randNum=random.randint(1, 20)
# Append this random number variable to the list using append() function
randomlist.append(randNum)
# print the random number list
print('The random number list which contains',
terms, 'elements = ', randomlist)


Output:

Enter the total number of elements in the given list =25
The random number list which contains 25 elements = [1, 6, 18, 5, 3, 2, 7, 5, 6, 6, 1, 2, 4, 17, 1, 20, 6, 2, 6, 19, 6, 10,
19, 20, 3]

Related Programs: