Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'faster :-)' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
faster :-)
Authored by: robh on Sep 06, '01 02:32:03PM

Just for fun..

perl -e '$dict = "/usr/share/dict/words"; $bytes= -s $dict; open IN, $dict;seek(IN,rand($bytes-11),0);$_=<IN>;$_=<IN>;print'

this uses the file size to compute the random number, and we then jump to that random byte offset in the file. Since we're unlikely to be at the beginning of a line, read and discard a line before reading and printing the next line.

Okay so it's not going to ever pick the first word in the file. I'll leave that fix as an exercise for the reader :-)

Anyhoo, using this method, the script is about 60 times faster.



[ Reply to This | # ]
faster :-)
Authored by: victory on Sep 06, '01 08:45:55PM
Nice! As an aside, the Perl implementation on OSX is now so standard that one could ostensibly go to any of the numerous Perl resources for info on the language. However anyone interested in Perl on OSX specifically might want to check out the macosx@perl.org mailing list. It's archived here.

[ Reply to This | # ]