Random Word Generator in Bash
danielbmarkham.com
danielbmarkham.com
Here it is since paragraph selection (triple click) doesn't exist in tap form on mobile. Added a few newlines because why not.
pickaword() {
WORDFREQFILE=/home/mywords.txt;
WORDLENGTH=$1;
FREQSUM=$(awk -v wordlength="$WORDLENGTH" 'length($1) == wordlength {s+=$2}END{print s}' "$WORDFREQFILE");
CHOICE=$(shuf -i 0-$FREQSUM -n 1);
awk -v wordlength="$WORDLENGTH" 'length($1) == wordlength {sum += $2; print $1, $2, sum} END {print sum}' "$WORDFREQFILE" | awk -v threshold="$CHOICE" '$3 < threshold' | tail -2 | head -1 | cut -f1 -d' ';
}
If you just want to generate random words (I use this from time to time) (note: do not use for cryptographic purposes!), you can just do `shuf /usr/share/dict/words | head -1` on most linux systems (since most have a dictionary installed; else you might have to apt install wamerican or something), but of course that doesn't take language frequency into account like OP has. To select a word length you could do `grep ^....$ /usr/share/dict/words | shuf | head -1` with the number of dots being the number of characters.