diff options
| author | Lex Neva <lexelby@users.noreply.github.com> | 2023-02-22 20:22:49 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-02-22 20:22:49 -0500 |
| commit | 8cdfaf099864e76a487ce77712ee54682e4a091f (patch) | |
| tree | 6fd43b320f53d4f81a40c0097091f8e4baaa31ac /lib/utils/list.py | |
| parent | e559af72480858c32f5df57f7f6a0d653718ebf8 (diff) | |
| parent | 92dff9f359f19c57ce63046a85cbd40746e50a32 (diff) | |
Merge pull request #1803 from inkstitch/lexelby/smoothing
Meander fill
Diffstat (limited to 'lib/utils/list.py')
| -rw-r--r-- | lib/utils/list.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/lib/utils/list.py b/lib/utils/list.py new file mode 100644 index 00000000..efa3969e --- /dev/null +++ b/lib/utils/list.py @@ -0,0 +1,23 @@ +import random + + +def _uniform_rng(): + while True: + yield random.uniform(0, 1) + + +_rng = _uniform_rng() + + +def poprandom(sequence, rng=_rng): + index = int(round(next(rng) * (len(sequence) - 1))) + item = sequence[index] + + # It's O(1) to pop the last item, and O(n) to pop any other item. So we'll + # always pop the last item and put it in the slot vacated by the item we're + # popping. + last_item = sequence.pop() + if index < len(sequence): + sequence[index] = last_item + + return item |
