// Outputs a randomly selected line from standard input with equal // likelihood. import std.random; import std.stdio;
void main() { auto n = 0; string choice; foreach (line; stdin.byLine()) { n++; if (uniform(0,n) == 0) choice = line.idup; } writeln(choice); } P.S. Proof that the probability any line is selected is exactly 1/n (where n is the total number of lines read) is left as an exercise for the reader. ;-) P.S.S. The .idup is a bit ugly, but necessary, since apparently byLine() calls readln() with a static buffer, so choice will be silently overwritten if the .idup is omitted. T -- Ruby is essentially Perl minus Wall.