MCPcopy Create free account
hub / github.com/ActiveState/code / irandomize

Function irandomize

recipes/Python/466177_Randomize_an_iterator/recipe-466177.py:1–14  ·  view source on GitHub ↗

Randomize the iterable. Note: this evaluates the entire iterable to a sequence in memory

(iterable, seed=None)

Source from the content-addressed store, hash-verified

1def irandomize(iterable, seed=None):
2 """
3 Randomize the iterable.
4
5 Note: this evaluates the entire iterable to a sequence in memory
6 """
7 if seed is None:
8 from random import shuffle
9 else:
10 from random import Random
11 shuffle = Random(seed).shuffle
12 result = list(iterable)
13 shuffle(result)
14 return iter(result)
15
16if __name__ == "__main__":
17 print list(irandomize(range(10)))

Callers 1

recipe-466177.pyFile · 0.85

Calls 2

listClass · 0.85
shuffleFunction · 0.50

Tested by

no test coverage detected