# Python – Non-overlapping Random Ranges

Sometimes, while working with Python, we can have problem in which we need to extract N random ranges which are non-overlapping in nature and with given range size. This can have applications in which we work with data. Lets discuss certain way in which this task can be performed.

Method : Using `any() + randint() `+ loop
This is brute force way in which this task can be performed. In this we extract random ranges using randint(), and check for non-repetition of number ranges using any() and loop.

 `# Python3 code to demonstrate working of  ` `# Non-overlapping Random Ranges ` `# Using loop + any() + randint() ` `import` `random ` ` `  `# initializing N  ` `N ``=` `7` ` `  `# initializing K  ` `K ``=` `5` ` `  `# Non-overlapping Random Ranges ` `# Using loop + any() + randint() ` `tot ``=` `10000` `res ``=` `set``() ` `for` `_ ``in` `range``(N): ` `  ``temp ``=` `random.randint(``0``, tot ``-` `K)  ` `  ``while` `any``(temp >``=` `idx ``and` `temp <``=` `idx ``+` `K ``for` `idx ``in` `res): ` `     ``temp ``=` `random.randint(``0``, tot ``-` `K)  ` `  ``res.add(temp) ` `res ``=` `[(idx, idx ``+` `K) ``for` `idx ``in` `res] ` ` `  `# printing result  ` `print``(``"The N Non-overlapping Random ranges are : "` `+` `str``(res))  `

Output :

```The N Non-overlapping Random ranges are : [(5347, 5352), (7108, 7113), (5479, 5484), (1906, 1911), (2228, 2233), (5206, 5211), (3260, 3265)]
```
My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.