blob: 063d7e1f75b96b53ed02fa3fa4f214aae49d3794 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
import itertools
import random
import typing as t
class RandomCycle:
"""Cycling through jumping to random indexes in an iterable."""
def __init__(self, iterable: t.Iterable) -> None:
self.iterable = list(iterable)
self.index = itertools.cycle(range(len(iterable)))
def __next__(self) -> t.Any:
idx = next(self.index)
if idx == 0:
random.shuffle(self.iterable)
return self.iterable[idx]
|