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

Random words from the terminal UNIX
Here's a nifty but somewhat useless quickie that will pick random words from the built-in dictionary file:
perl -e 'open IN, "</usr/share/dict/words";rand($.) < 1 && ($n=$_) while <IN>;print $n'
  • Inspired by an example in the 'Camel' book and the fact that I couldn't come up with a non-Bash way to generate random numbers from the shell. (Any ideas why there's no /dev/random? It kinda seems that this was a design decision rather than an oversight...)
  • The /usr/share/dict/words exists on most Unix systems. Curiously, the words seem to be different from that of the system-wide spellchecker service built into OSX.
  • You can use the standard backtick operator to plop the word into another cmd-line program.
[To the editor: I admit this is sort of off-topic as far as Mac OSX is concerned. Perhaps you can use it when there's a dry spell in hints or as a 'stupid shell tricks' feature.]

[From the editor: Hey, it is a bit slow with everyone awaiting 10.1's arrival, and this is an interesting trick. There are some strange words in the dictionary!]
  • Currently 0.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (0 votes cast)

Random words from the terminal | 4 comments | Create New Account
Click here to return to the 'Random words from the terminal' 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 mailing list. It's archived here.

[ Reply to This | # ]
/dev/random is Not Unix
Authored by: spyro_le_dragon on Sep 07, '01 04:17:42PM

I think the /dev/random (and some other /dev things) is purely
a Linux feature and does not exist in other Unixes (in particular
in FreeBSD and therefore darwin)
But I may be wrong in the special case of /dev/random
am I?

[ Reply to This | # ]
Multiple Random words from the terminal
Authored by: sophistry on Mar 14, '03 11:27:45AM

Looking over the old entries... here's some more fun to have with the
dictionary. This script lets you get groups of words all of the same

jot -r -c 1000000 a z | rs -g 0 6 | sort | join /usr/share/dict/words - |

[ Reply to This | # ]