Random sample() Method in Python:
The sample() method returns a list containing a randomly selected number of items from a sequence.
Note: Please keep in mind that this method does not alter(change) the original sequence.
Syntax:
random.sample(sequence, size)
Parameters
sequence: This is Required. It could be any sequence like a list, a tuple, a range of numbers, and so on.
size: This is Required. The number of items in the returned list.
Examples:
Example1:
Input:
Given list = [4, 7, 9, 0, 1] Given size = 3
Output:
The 3 random items from a given list = [1, 9, 7]
Example2:
Input:
Given list = ["hello", "this", "is", "btechgeeks"] Given size = 2
Output:
The 2 random items from a given list = ['is', 'btechgeeks']
Random sample() Method with Examples in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Import random module using the import keyword.
- Give the list as static input and store it in a variable.
- Give the size of the list to be returned as static input and store it in another variable.
- Pass the given list, size as the arguments to the random.sample() method to get the given number(size) of random items from a given list
- Store it in another variable.
- Print the given number of random items from a given list
- The Exit of the Program.
Below is the implementation:
# Import random module using the import keyword.
import random
# Give the list as static input and store it in a variable.
gvn_lst = [4, 7, 9, 0, 1]
# Give the size of the list to be returned as static input and store it in
# another variable.
gvn_size = 3
# Pass the given list, size as the arguments to the random.sample() method to
# get the given number(size) of random items from a given list
# Store it in another variable.
rslt = random.sample(gvn_lst, gvn_size)
# Print the given number of random items from a given list
print("The", gvn_size, "random items from a given list = ", rslt)
Output:
The 3 random items from a given list = [1, 9, 7]
Note: Similarly, you can also try for strings, to get the given number of random characters from a given string.
Method #2: Using Built-in Functions (User Input)
Approach:
- Import random module using the import keyword.
- Give the list as user input using list(),map(),input(),and split() functions.
- Store it in a variable.
- Give the size of the list to be returned as user input using the int(input()) function and store it in another variable.
- Pass the given list, size as the arguments to the random.sample() method to get the given number(size) of random items from a given list
- Store it in another variable.
- Print the given number of random items from a given list
- The Exit of the Program.
Below is the implementation:
# Import random module using the import keyword.
import random
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
'Enter some random List Elements separated by spaces = ').split()))
# Give the size of the list to be returned as user input using the int(input())
# function and store it in another variable.
gvn_size = int(input("Enter some random number = "))
# Pass the given list, size as the arguments to the random.sample() method to
# get the given number(size) of random items from a given list
# Store it in another variable.
rslt = random.sample(gvn_lst, gvn_size)
# Print the given number of random items from a given list
print("The", gvn_size, "random items from a given list = ", rslt)
Output:
Enter some random List Elements separated by spaces = 23 10 20 30 40 50 Enter some random number = 2 The 2 random items from a given list = [50, 20]