Open In App

Python – N Random Tuples list

Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python records, we can have a problem in which we need to construct a random tuple list. This can have applications in many domains using gaming and day-to-day programming. Let’s discuss specific ways in which this task can be performed in Python.

Input:  N = 4 R = 6 
Output: [(5, 6), (1, 0), (1, 3), (2, 3)] 

Input:  N = 8 R = 4 
Output: [(2, 3), (4, 1), (2, 0), (4, 3), (4, 2), (0, 2), (1, 4), (0, 1)] 

Explanation: In this, we are printing N Random Tuple List in Python.

Randomly select an item from a tuple using list comprehension + sample()

This is one of the ways in which this task can be performed. In this, random number generation takes place using sample() which, when given pool of numbers extracts random numbers for forming pairs, which are paired using list comprehension.

Python3




import random
 
# initializing N
N = 5
 
# initializing Tuple element range
R = 10
 
# N Random Tuples list
# Using list comprehension + sample()
res = [divmod(ele, R + 1) for ele in random.sample(range((R + 1) * (R + 1)), N)]
 
# printing result
print("The N random tuples : " + str(res))


Output:

The N random tuples : [(10, 9), (7, 4), (1, 10), (9, 10), (7, 5)]

Randomly select an item from a tuple using Product() + Sample()

The combination of above functions can be used to solve this problem. In this, we perform task of producing numbers in a range using product() and sample() is used to extract N random numbers from them.

Python3




# Python3 code to demonstrate working of
# N Random Tuples list
# Using product() + sample()
import random
import itertools
 
# initializing N
N = 5
 
# initializing Tuple element range
R = 10
 
# N Random Tuples list
# Using product() + sample()
res = random.sample(list(itertools.product(range(R + 1), repeat = 2)), N)
 
# printing result
print("The N random tuples :" + str(res))


Output:

The N random tuples :[(4, 5), (10, 0), (2, 7), (5, 2), (8, 0)]

Randomly select an item from a tuple using randint()+lambda

To generate and print N random tuples using the randint() function along with a lambda function, you can use the random module in Python. This approach allows you to create random tuples with specified ranges and sizes using a lambda function. Define an anonymous function (generate_tuple) that generates a random tuple of two elements, where each element is a random integer between 0 (inclusive) and R (inclusive). Create a list comprehension to generate N random tuples using the generate_tuple function.

Python3




import random
 
N = 4
R = 6
 
generate_tuples = lambda N, R: [(random.randint(0, R), random.randint(0, R)) for _ in range(N)]
 
output = generate_tuples(N, R)
print(output)


Output:

[(2, 3), (1, 3), (5, 2), (1, 6)]

Time Complexity: O(N), where N is no of tuples
Space Complexity: O(N), where N is no of tuples



Last Updated : 28 Aug, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads