-
Notifications
You must be signed in to change notification settings - Fork 0
/
05 - random_int_array.py
36 lines (25 loc) · 1.62 KB
/
05 - random_int_array.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import random
def main():
min_value = int(input("Введите минимальное значение: "))
max_value = int(input("Введите максимальное значение: "))
count = int(input("Введите количество случайных элементов: "))
repeat = input("Могут ли числа повторяться? (y или n): ")
print(random_array(min_value, max_value, count, repeat))
def random_array(min, max, count, rep):
# Проверяем, не ввёл ли пользователь количество элементов больше, чем возможно уникальных чисел в указанном диапазоне.
if (rep == "n") and (count > max - min + 1):
print("Запрашиваемое количество элементов превышает допустимый диапазон. Будут выведены все числа указанного диапазона в случайном порядке.")
count = max - min + 1
result_array = []
for index in range(0, count):
if rep == "y":
result_array.append(random.randint(min, max))
else:
# Пока не получим уникальное значение, повторяем генерацию случайного числа.
rand_number = random.randint(min, max)
while rand_number in result_array:
rand_number = random.randint(min, max)
else:
result_array.append(rand_number)
return result_array
main()