Select a random element from a set in Python

Select a random element from a set in Python 

Select a random element from a set in Python

  Selecting a random element from a group of samples is a deterministic task. A computer program can simulate such a random selection simulation using a pseudo-random number generator. In this article, we will learn how to select a random element from a set in Python.

  What is a set in Python?

A set is an unordered collection of elements that are repeatable, mutable, and have no repeating elements. The elements in a set can be heterogeneous. Sets use a structure known as a hash table to look up a given element, making them faster and more efficient to process than a list. Because sets are unordered, we can't access elements using indexes, as we can in lists. The set syntax is as follows:


variable_name = {element_one, element_two, element_three}

Where element_one/two/three is any data type present in Python.

Picking a random element from a set

Selecting a random element from multiple elements can be done using a pseudo-random number generator. For this, functions from the random library will be used.

Using the select function

The selection function selects a random element from a non-empty sequence. The function takes a sequence as an argument and returns a random element from it. The following example demonstrates the use of the select function to select a random element from a predefined set:

import random

 

# A set containing elements of different datatype

set = (1, "Hello", 38, 44.45, "Apples", False) 

 

print("An element from the set:" , random.choice(set))

output:

An element from the set: 38

Explanation:


First, a set is defined as containing multiple elements of different data types. The select function is then called and the set is passed as an argument. The function returns a random element from the set.


Using the ranking function

The randrange function selects a random element from a given range. The function takes a range (or end point) as an argument and returns a random element from the range, regardless of bounds. The following example shows the use of the randrange function to select a random element from a predefined set:

import random

 

# A set containing elements of different datatype

set = (1, "Hello", 38, 44.45, "Apples", False)

 

# Passing the length of the set as an argument to the function

# This produces a random integer between the index 0 to Len-1

element = random.randrange(, len(set))

 

# Obtaining a element from the list based on its index

# Where the index is obtained randomly

print("An element from the set:", set[element])

output:


An element from the set: 38

Explanation:


First, the same set was defined as in the previous example. The randrange function is then called and the length of the set is passed as an argument. The function generates a random integer in the given range, which is stored in a variable. Finally, it displays the element associated with that index (denoted by a random integer) within the set.

Please leave your comment to encourage us

Previous Post Next Post