Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- def scramble(gen, buffer_size):
- buf = []
- i = iter(gen)
- while True:
- try:
- e = next(i)
- buf.append(e)
- if len(buf) >= buffer_size:
- choice = random.randint(0, len(buf)-1)
- buf[-1], buf[choice] = buf[choice], buf[-1]
- yield buf.pop()
- except StopIteration:
- random.shuffle(buf)
- yield from buf
- return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement