aboutsummaryrefslogtreecommitdiffstats
path: root/bot/utils
diff options
context:
space:
mode:
authorGravatar Anubhav1603 <[email protected]>2020-09-22 17:59:02 +0530
committerGravatar Anubhav1603 <[email protected]>2020-09-22 17:59:02 +0530
commitb32293eeb0e069f495e5b9acfcde47523c7cb8be (patch)
tree265b95d12c1e71ce7ca0a3c675a43e3d13aacbfa /bot/utils
parentadded subcommand date and removed a from doc string of date (diff)
parentMerge pull request #430 from Anubhav1603/master (diff)
Merge branch 'master' of https://github.com/python-discord/seasonalbot into partnerzodiac
Diffstat (limited to 'bot/utils')
-rw-r--r--bot/utils/randomization.py23
1 files changed, 23 insertions, 0 deletions
diff --git a/bot/utils/randomization.py b/bot/utils/randomization.py
new file mode 100644
index 00000000..8f47679a
--- /dev/null
+++ b/bot/utils/randomization.py
@@ -0,0 +1,23 @@
+import itertools
+import random
+import typing as t
+
+
+class RandomCycle:
+ """
+ Cycles through elements from a randomly shuffled iterable, repeating indefinitely.
+
+ The iterable is reshuffled after each full cycle.
+ """
+
+ 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]