Skip to content

Instantly share code, notes, and snippets.

@jenkspt
Created June 7, 2018 23:45
Show Gist options
  • Save jenkspt/beb65d02c958cd0648510f1716e6083e to your computer and use it in GitHub Desktop.
Save jenkspt/beb65d02c958cd0648510f1716e6083e to your computer and use it in GitHub Desktop.
Randomly shuffles an iterator
import random
def shuffle(iterator, buffer_size):
""" Uses a buffer to randomly shuffle items from an iterator """
# Fill the buffer
buffer = []; cnt = 0
item = next(iterator, None)
while not item is None and cnt < buffer_size:
buffer.append(item)
item = next(iterator, None)
cnt += 1
random.shuffle(buffer)
print('beginning iteration')
while not item is None:
# Randomly select from the buffer
index = random.randint(0, cnt-1)
yield buffer[index]
# Replace the selected item
buffer[index] = item
item = next(iterator, None)
# Empty the buffer
while buffer:
yield buffer.pop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment