Python - Get a sorted list of random integers with unique elements
Last Updated :
17 Apr, 2023
Given lower and upper limits, generate a sorted list of random numbers with unique elements, starting from start to end.
Examples:
Input: num = 10, start = 100, end = 200
Output: [102, 118, 124, 131, 140, 148, 161, 166, 176, 180]
Input: num = 5, start = 1, end = 100
Output: [37, 49, 64, 84, 95]
To generate random numbers in Python, randint() function of random module is used.
Syntax:
randint(start, end)
randint() accepts two parameters: a starting point and an ending point. Both should be integers and the first value should always be less than the second. Below is the implementation.
Python3
# Python program to create
# a sorted list of unique random
# numbers
import random
# Function to generate a sorted list
# of random numbers in a given
# range with unique elements
def createRandomSortedList(num, start = 1, end = 100):
arr = []
tmp = random.randint(start, end)
for x in range(num):
while tmp in arr:
tmp = random.randint(start, end)
arr.append(tmp)
arr.sort()
return arr
# Driver's code
print(createRandomSortedList(10, 100, 200))
print(createRandomSortedList(5)))
Output:
[102, 118, 124, 131, 140, 148, 161, 166, 176, 180]
[37, 49, 64, 84, 95]
Time complexity: O(nlogn), where n is the length of the test_list.
Auxiliary Space: O(n), where n is the number of elements in the arr
Note: Install numpy module using command "pip install numpy"
Another approach would be to use the numpy library to generate random unique integers within a given range and then sort the resulting array.
Python3
import numpy as np
# Python program to create
# a sorted list of unique random
def createRandomSortedList(num, start = 1, end = 100):
arr = np.random.choice(np.arange(start, end + 1), size=num, replace=False)
arr.sort()
return arr
# Driver's code
print(createRandomSortedList(10, 100, 200))
print(createRandomSortedList(5))
Output:
[102 105 106 112 114 119 129 152 176 198]
[ 4 62 77 86 98]
This approach uses the numpy function random.choice() to generate an array of random integers between start and end (inclusive) of size num without replacement. Time complexity is O(n) as it takes linear time to sort the array of size n. Space complexity is O(n) as it creates an array of size n.
Similar Reads
Randomly Select N Elements from List in Python When working with lists in Python, we often need to randomly select a specific number of elements. For example, consider the list a = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]. We might want to randomly select 3 elements from this list. Let's discuss different ways for selecting n random elements fr
3 min read
Randomly select elements from list without repetition in Python We are given a list, and our task is to randomly select elements from it without repetition. This means that once an element is selected, it should not be picked again. For example, given a = [10, 20, 30, 40, 50], selecting three elements randomly could result in [30, 10, 50], but the same element s
2 min read
How to randomly select elements of an array with NumPy in Python ? Randomly selecting elements from an array means choosing random elements from the array. NumPy offers several efficient methods to pick elements either with or without repetition. For example, if you have an array [1, 2, 3, 4, 5] and want to randomly select 3 unique elements, the output might look l
2 min read
Python - Generate random numbers within a given range and store in a list Our task is to generate random numbers within a given range and store them in a list in Python. For example, you might want to generate 5 random numbers between 20 and 40 and store them in a list, which could look like this: [30, 34, 31, 36, 30]. Let's explore different methods to do this efficientl
3 min read
How to create a matrix of random integers in Python ? Prerequisites: numpy To create a matrix of random integers in Python, randint() function of the numpy module is used. This function is used for random sampling i.e. all the numbers generated will be at random and cannot be predicted at hand. Syntax : Â numpy.random.randint(low, high=None, size=None,
2 min read