In the previous article, we have discussed Python Program to Get n Random Items from a List
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 = 3 Given tuple = ("btechgeeks", 321, "good morning", [7, 8, 5, 33], 35.8)
Output:
Example 2:
Input:
Given no of random numbers to be generated = 4 Given tuple = (255, "hello", "btechgeeks", 150,"good morning", [1, 2, 3, 4], 100, 98)
Output:
The given 4 Random numbers are : [1, 2, 3, 4] 255 255 98
Program to Select a Random Element from a Tuple
Below are the ways to select a random element from a given Tuple.
Method #1: Using random.choice() Method (Only one element)
Approach:
- Import random module using the import keyword.
- Give the tuple as static input and store it in a variable.
- Apply random. choice() method for the above-given tuple and store it in another variable.
- Print the random element from the above-given tuple
- The Exit of the program.
Below is the implementation:
# Import random module using the import keyword. import random # Give the tuple as static input and store it in a variable. gvn_tupl = (255, "hello", "btechgeeks", 150, "good morning", [1, 2, 3, 4], 100, 98) # Apply random.choice() method for the above given tuple and store it in another variable. reslt = random.choice(gvn_tupl) # Print the random element from the above given tuple print("The random element from the above given tuple = ", reslt)
Output:
The random element from the above given tuple = btechgeeks
Method #2: Using For Loop (N elements)
Approach:
- Import random module using the import keyword.
- Give the number as static input and store it in a variable.
- Give the tuple as static input and store it in another variable.
- Loop in the given tuple above given ‘n’ number of times using For loop.
- Inside the loop, apply random.choice() method for the above-given tuple and store it in a variable.
- Print the given number of random elements to be generated from the given tuple.
- 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 tuple as static input and store it in another variable. gvn_tupl = ("btechgeeks", 321, "good morning", [7, 8, 5, 33], 35.8) print("The given", randm_numbrs, "Random numbers are :") # Loop in the given tuple 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 tuple and # store it in a variable. reslt = random.choice(gvn_tupl) # Print the given numbers of random elements to be generated. print(reslt)
Output:
The given 3 Random numbers are : btechgeeks [7, 8, 5, 33] 321
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.
- Python Program to Shuffle Elements of a Tuple
- Python Program to Remove Elements from a Tuple
- Python Program to Find the Size of a Tuple
- Python Program to Union of Set of Tuples