Python - Non-overlapping Random Ranges


The problem at hand is to find the non overlapping random ranges with the help of Python. Sometimes in Python we need to extract random ranges which are non overlapping. This application will be helpful while working with data.

Understanding the Problem

In this problem statement we will be given three parameters like starting values, ending value and also the number range. So on the basis of these values we will have to generate the non-overlapping random ranges using Python. Suppose we have a starting value as 1 and ending value is 50 and number range is 2 so the Output will be [(8, 13), (43, 47)].

Logic for The Above Problem

To solve this problem we will be using the random module of Python. So first we will create a function to do this task. And inside the function we will initialize the empty object to store the resultant values. And then using the loop which will run until the number ranges. And we will get the values of beginning and ending ranges using the random.randint function. And then append both the values as a tuple.

Algorithm

  • Step 1 − Import the necessary module in the program first. In our program we are using random module.

  • Step 2 − Then define the function called get_ranges(). Inside this function we will pass three parameters as begin, end, and num_ranges.

  • Step 3 − Next we will initiate an empty object called random_ranges. This list will store the non-overlapping random ranges.

  • Step 4 − A loop will be initiated that iterates num_ranges times. And inside this loop we will generate a random integer called range_begin and range_end with the help of random.randint() function. These values will show the starting and ending point of the range within the given range limits.

  • Step 5 − After having range_begin and range_end values. We will create a tuple which will represent the generated range and append this value to the ranges list.

  • Step 6 − At the end we will return the list of non-overlapping ranges to show on the console.

Example

# Import the random module
import random

# Define the function to generate non overlapping ranges
def get_ranges(begin, end, num_ranges):

   # Initialize an empty object
   random_ranges = []
   
   # Iterate
   for _ in range(num_ranges):
      range_begin = random.randint(begin, end)
      range_end = random.randint(range_begin, end)
      random_ranges.append((range_begin, range_end))
   return random_ranges
   
# Initialize the variables
begin = 10
end = 500
num_ranges = 5

# Call the function
non_overlapping = get_ranges(begin, end, num_ranges)
print(non_overlapping)

Output

[(26, 450), (498, 499), (149, 259), (300, 491), (26, 454)]

Complexity

The time complexity of the function called get_ranges() is O(n), here n is the number of num_ranges. As we have iterated to generate two random integers of beginning and ending numbers and created a tuple by appending both the values. The space complexity for this code is O(2n).

Conclusion

As we have generated a straightforward approach to generate non-overlapping random ranges using Python. This scenario can be used to generate a set of distinct ranges within the given range.

Updated on: 17-Oct-2023

50 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements