Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.44 KB | None | 0 0
  1. import random
  2.  
  3. def scramble(gen, buffer_size):
  4. buf = []
  5. i = iter(gen)
  6. while True:
  7. try:
  8. e = next(i)
  9. buf.append(e)
  10. if len(buf) >= buffer_size:
  11. choice = random.randint(0, len(buf)-1)
  12. buf[-1], buf[choice] = buf[choice], buf[-1]
  13. yield buf.pop()
  14. except StopIteration:
  15. random.shuffle(buf)
  16. yield from buf
  17. return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement