import os import random import sys def random_items(iterator, items_wanted=1): '''Select items at random from an iterator. Each item has an equal chance of being picked. Each item may be picked more than once. The iterator is processed only once, and only selected items are stored, making this function memory efficient. Idea from Richard Papworth - see http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/59865''' selected_items = [None] * items_wanted for item_index, item in enumerate(iterator): for selected_item_index in xrange(items_wanted): if not random.randint(0, item_index): selected_items[selected_item_index] = item return selected_items if __name__ == '__main__': # Test - run over this source. ;-) file_name = sys.argv[0] file_iterator = (line.rstrip() for line in open(file_name)) selected_items = random_items(file_iterator, 5) print os.linesep.join(selected_items)