# Python Program to Get n Random Items from a List

In the previous article, we have discussed Python Program Divide all Elements of a List by a Number
Random Module in python :

As this Random module is one of Python’s predefined modules, its methods return random values.

It selects integers uniformly from a range. For sequences, it has a function to generate a random permutation of a list in-place, as well as a function to generate a random sampling without replacement. Let’s take a look at how to import the Random Module.

The random module in Python is made up of various built-in Methods.

choice():Â  choice() is used to select an item at random from a list, tuple, or other collection.

Because the choice() method returns a single element, we will be using it in looping statements.
sample(): To meet our needs, we’ll use sample() to select multiple values.

Examples:

Example1:

Input:

Given no of random numbers to be generated = 5
Given list =[1, 2, 3, 2, 2, 1, 4, 5, 6, 8, 9]

Output:

The given 5 Random numbers are :
2
3
8
6
9

Example 2:

Input:

Given no of random numbers to be generated = 3
Given list = [2, 1, 6, 1, 4, 5, 6, 8]

Output:

The given 3 Random numbers are :
6
5
1

## Program to Get n Random Items from a List

Below are the ways to Get n Random Items from a Given List.

### Method #1: Using random.choice() Method (Static input)

Approach:

• Import random module using the import keyword.
• Give the number as static input and store it in a variable.
• Give the list as static input and store it in another variable.
• Loop in the given list above given ‘n’ number of times using For loop.
• Inside the loop, apply random.choice() method for the above-given list and store it in a variable.
• Print the given number of random numbers to be generated.
• The Exit of the program.

Below is the implementation:

# Import random module using the import keyword.
import random
# Give the number as static input and store it in a variable.
randm_numbrs = 3
# Give the list as static input and store it in another variable.
gvn_lst = [1, 2, 3, 2, 2, 1, 4, 5, 6, 8, 9]
print("The given", randm_numbrs, "Random numbers are :")
# Loop above given 'n' number of times using For loop.
for itr in range(randm_numbrs):
# Inside the loop, apply random.choice() method for the above given list and
# store it in a variable.
reslt = random.choice(gvn_lst)
# Print the given numbers of random numbers to be generated.
print(reslt)


Output:

The given 3 Random numbers are :
5
3
4

### Method #2: Using random.choice() Method (User input)

Approach:

• Import random module using the import keyword.
• Give the number as user input using int(input()) and store it in a variable.
• Give the list as user input and using list(),map(), int(),input(),and split() functions store it in another variable
• The loop above given ‘n’ number of times using For loop.
• Inside the loop, apply random.choice() method for the above-given list and store it in a variable.
• Print the given number of random numbers to be generated.
• The Exit of the program.

Below is the implementation:

# Import random module using the import keyword.
import random
# Give the number as user input using int(input()) and store it in a variable.
randm_numbrs = int(input("Enter some random number = "))
#Give the list as user input and using list(),map(), int(),input(),and split() functions
#store it in another variable
gvn_lst = list(map(int, input(
'Enter some random List Elements separated by spaces = ').split()))
print("The given", randm_numbrs, "Random numbers are :")
# Loop above given 'n' number of times using For loop.
for itr in range(randm_numbrs):
# Inside the loop, apply random.choice() method for the above given list and
# store it in a variable.
reslt = random.choice(gvn_lst)
# Print the given numbers of random numbers to be generated.
print(reslt)


Output:

Enter some random number = 4
Enter some random List Elements separated by spaces = 1 2 3 4 5 6 7 8 9
The given 4 Random numbers are :
2
2
4
8

Explore more instances related to python concepts fromÂ Python Programming ExamplesÂ Guide and get promoted from beginner to professional programmer level in Python Programming Language.