#!/usr/bin/env python # randomline: echo one line, randomly chosen, from stdin. # Copyright 2007 Akkana Peck, GPL blah blah, free to reuse blah blah # # Thanks to saz and http://www.goldfish.org/books/O'Reilly%20Perl%20CD%20Bookshelf%202.0/cookbook/ch08_07.htm # for the cute algorithm. import sys, string, random # main random.seed() num_lines = 0 picked = "" while 1 : line = sys.stdin.readline() if not line : break line = string.strip(line, " \n") if line == '' : continue num_lines += 1 if random.randint(1, num_lines) == 1 : picked = line print picked