aboutsummaryrefslogtreecommitdiffstats
path: root/bot/utils
diff options
context:
space:
mode:
authorGravatar Xithrius <[email protected]>2020-09-09 15:49:05 -0700
committerGravatar Xithrius <[email protected]>2020-09-09 15:49:05 -0700
commit434e9a65529d77a0954255781c8792de60ab28d6 (patch)
treef534047a431b71b329f7699591c9fb1a11b4efb9 /bot/utils
parentRemoved logging. (diff)
Added RandomCycle utility to jump between set indexes repeatedly.
Diffstat (limited to 'bot/utils')
-rw-r--r--bot/utils/randomization.py19
1 files changed, 19 insertions, 0 deletions
diff --git a/bot/utils/randomization.py b/bot/utils/randomization.py
new file mode 100644
index 00000000..063d7e1f
--- /dev/null
+++ b/bot/utils/randomization.py
@@ -0,0 +1,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]