Python – 获取具有唯一元素的随机整数的排序列表
给定下限和上限,从头到尾生成具有唯一元素的随机数的排序列表。
例子:
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]
为了在Python中生成随机数,使用了random
模块的randint()
函数。
句法:
randint(start, end)
randint()
接受两个参数:起点和终点。两者都应该是整数,第一个值应该总是小于第二个。
下面是实现。
# 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)))
输出:
[102, 118, 124, 131, 140, 148, 161, 166, 176, 180]
[37, 49, 64, 84, 95]