How can I fetch random words from an electronic dictionary database or text file?

If the wordlist is a plain text file with one word per line, then one option is the shuf command e.g.

$ shuf -n5 /usr/share/dict/american-english
resuscitated
Lawson
concatenate
nonsmoker's
balmiest

See man shuf

SHUF(1)                          User Commands                         SHUF(1)

NAME
       shuf - generate random permutations

SYNOPSIS
       shuf [OPTION]... [FILE]
       shuf -e [OPTION]... [ARG]...
       shuf -i LO-HI [OPTION]...

DESCRIPTION
       Write a random permutation of the input lines to standard output.

I'd favour shuf for plucking lines from a file. We also need to be careful with the words because it's a tab-delimited file, not just whitespace.

$ shuf -n5 /usr/share/dictd/wn.index | cut -d$'\t' -f1
table game
parsi
tetraneuris grandiflora
synonymously
decimal digit

shuf takes 0.011s here. sort -R (even without the awk) takes 2.427s. This is because shuf is just seeking through the file n times while sort is reading and processing every line... In this case that's 147311 lines.


There are plenty of ways to get a random word. Example to retrieve five words:

$ sort -R /usr/share/dictd/freedict-nld-eng.index | awk 'NR <= 5 { print $1 }'
verslappen
sport
libretto
golf
kerk

See man sort for the meaning of the -R option (hint: Random). The awk expression filters on the first five records (lines) and prints the first field (not the other columns).