случайным образом нужно выбрать 4 элемента из списка чтобы они НЕ ПОВТОРЯЛИСЬ, есть ли такая возможность?
import random
lots = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 'a', 'b', 'c', 'd', 'e']
random_index = len(lots) * random.randint(4, 4) // 15
print("The winner of lotery need to have this index:")
print(random.choices(lots, k=random_index))
Оно работает, но иногда выходят два одинаковых индекса, а не хотелось бы. Спасибо большое!
Ответы (2 шт):
Автор решения: m4rkiz.nik
→ Ссылка
Я все понял у choices() и есть свойство повторяться.
Вот простое решение:
import random
lots = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 'a', 'b', 'c', 'd', 'e']
print(random.sample(lots, 4))
Автор решения: Struckture
→ Ссылка
Попробуйте это.
import random
lots = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 'a', 'b', 'c', 'd', 'e']
result = set()
while len(result) < 4:
my_index = lots[random.randint(0, len(lots) - 1)]
for i in result:
print("The winner of lotery need to have this index:", i)