#!/usr/local/bin/python # A really cool example: Uses a set to generate non-repeating "random" numbers # Note the use of random, sets, and exceptions import random def random_norep(low,high): candidate = random.randint(low,high) used = set ([ candidate ]) # Note the syntax, we haven't used sets much yield candidate while ( ((high-low)+1) > len(used)): candidate = random.randint(low,high) if (candidate in used): continue used.add(candidate) yield candidate rands = random_norep(0,9) print rands.next() print rands.next() print rands.next() """ try: while True: print rands.next() except StopIteration: print "All done -- we've used all of the numbers in the requested range." """