Both work also for a list of non-ideal elements:
def choice_without_repetition(lst):
prev = None
while True:
i = random.randrange(len(lst))
if i != prev:
yield lst[i]
prev = i
or
def choice_without_repetition(lst):
i = 0
while True:
i = (i + random.randrange(1, len(lst))) % len(lst)
yield lst[i]
Using:
lst = [1,2,3,4,5,6,7,8]
for x in choice_without_repetition(lst):
print x
source
share