2023-01-18 02:44:23 +00:00
|
|
|
import random
|
2023-01-16 19:27:06 +00:00
|
|
|
|
|
|
|
|
2023-01-18 02:44:23 +00:00
|
|
|
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)))
|
2023-01-16 19:27:06 +00:00
|
|
|
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
|
2024-06-20 17:45:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
def is_all_zeroes(sequence):
|
|
|
|
return all(item == 0 for item in sequence)
|