Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!rutgers!apple!motcsd!hpda!hpcupt1!hpisod2!decot From: decot@hpisod2.HP.COM (Dave Decot) Newsgroups: comp.unix.questions Subject: Re: How do I pick a word at random from a list of words? Message-ID: <10650054@hpisod2.HP.COM> Date: 19 May 89 18:27:14 GMT References: <19669@adm.BRL.MIL> Organization: Hewlett Packard, Cupertino Lines: 39 Well, this program gives you a random line from a file. The first argument to the program should be the number of lines in the file. Compile this program as "select", and use it as: select `wc -l file` < file Unfortunately, this only works on systems with gettimeofday(), rand(), and srand() (such as BSD-derived systems or HP-UX); substitute your local random number generators and time functions. Dave Decot ------------------------ #include #include main(argc, argv) int argc; char *argv[]; { char s[80]; struct timeval t; int i, j; gettimeofday(&t, 0); j = srand(t.tv_usec); freopen(argv[2], "r", stdin); gets(s); i = rand() % atoi(argv[1]) - 1; while(i-- > 0) gets(s); puts(s); }