in reply to RandomFile

While I was thinking about methods that don't involve preprocessing, a nice solution to your problem hit me smack in the face. I didn't even see it coming.

You could create a file that includes nothing but the locations of the beginning of each line in /usr/dict/words. It would be big, but it would give you simple, efficient, and very effective solutions to your problem and many similar ones.

For example, your problem would be solved by the following:

1. Read a certain number of random locations from the index file (easy with constant-length records).
2. Check each location (in /usr/dict/words) until you find an n-letter word. If you don't find one, go back to step 1.

The number you of locations you read in step 1 would have to be optimized for performance (reading time vs. how often the algorithm must be restarted), and might be variable by the length of word you want. For instance, you could hard-code an array @num in your script so that reading $num(n) locations would locate an n-letter word 95% of the time, and the algorithm would be restarted the remaining 5% of the time. The numbers wouldn't be very big, and you wouldn't have to be right on the optimum solution to get good performance.

The best thing about this solution is that the index file would be useful for almost any program that needs to get random words from a certain subset of /usr/dict/words. If you have any other problems similar to the one you posted, this might be the way to go.