How to select a Random element from a Tuple in Python

In this article, we will study how we can randomly select any element from Tuple using Python as Programming Language. For this task, we need to understand the concept of Random Selection because it is essential.

Have you ever played with Dice? Dice has 6 faces numbered from 1 to 6. Every time we throw the Dice, different results are seen. Any random number from 1 to 6 comes as an output on throwing a Dice. We may get the same result or different result but that is randomly generated.

How to select a Random element from a Tuple in Python

Similar concept we use to select Random Element from Tuple, List or String in Python.

Instead of Dice, Python provides us with Module called Random Module which works in similar fashion as dice works. The random module is used to get access to various random functions like random.random(), random.randint(), random.choice() etc.

Selection of Random Element from Tuple in Python

Let us now look how selection of Random Element in Tuple takes place using Python.

1) Using random.choice(seq)

  • STEP 1: Import Random Module

Using Random Module we can get access to a variety of functions to do with random number generation. We need to import this module into our code so that we can use functions embedded in Random module.

import random
  • STEP 2: Create a Tuple

Create a Tuple on which Random module has to be implemented. Tuple is used to store immutable objects. Immutable means whose value cannot change.Since, we are learning how to select random element from tuple, a tuple is created. We can use list or string as per our need.

elementsOfTuple = ("Codespeedy",23,[3,4,5],99.44)
  • STEP 3: Use random.choice(seq)

Use random.choice(seq) which is inbuilt function in Random Module. It will return  the randomly selected element. “seq” could be list, tuple or string but should not be empty. choice() is called using random static object.

print("Randomly selected element is:",random.choice(elementsOfTuple))

OUTPUT

Randomly selected element is: 23

Following is the pictorial representation of random.choice(seq).

select a Random element from a Tuple

Let us at look code to select and return random element from tuple “n” times.

for i in range (10):
    print(i,"=>","Random element:",random.choice(elementsOfTuple))

OUTPUT

0 => Random element: Codespeedy
1 => Random element: Codespeedy
2 => Random element: 23
3 => Random element: Codespeedy
4 => Random element: Codespeedy
5 => Random element: 99.44
6 => Random element: 99.44
7 => Random element: Codespeedy
8 => Random element: [3, 4, 5]
9 => Random element: 99.44

II) Using random.sample(seq,size)

  • STEP 1: Import Random Module
import random
  • STEP 2: Create a Tuple as above

Tuple can be declared empty but while using random.choice(seq) we should never declare it empty since it will raise an error. This is because choice() cannot choose from an empty sequence.

elementsOfTuple = ("Codespeedy",23,[3,4,5],99.44)
  • STEP 3: Use random.sample(seq,size) to return result

Use random.sample(seq,size) which is inbuilt function in Random Module. It will return the randomly selected element. “seq” could be list,tuple or string but should not be empty. The number of items returned from the sequence is size . It is an integer value. sample() is accessed through random object and it cannot be accessed directly.

print("Randomly selected element is:",random.sample(elementsOfTuple,3))

OUTPUT

Randomly selected element is: [99.44, [3, 4, 5], 'Codespeedy']

Thank You.

You may also read: Tuples in Python with examples

Leave a Reply

Your email address will not be published. Required fields are marked *