Hacker News new | past | comments | ask | show | jobs | submit login
Random Word Generator in Bash (danielbmarkham.com)
16 points by todsacerdoti on March 19, 2022 | hide | past | favorite | 4 comments



The code renders correctly neither on Firefox (desktop) nor mobile (some webview browser). It goes out of the screen both left and right.

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.


shuf has flag to make it use an alternative source for randomness. If we pass in a secure source of randomness such as /dev/urandom would shuf work for crypto purposes?


Potentially, but since it's not advertised as such (at least I don't see it in the man page) I would not rely on it without someone actually checking the source code properly.


Fixed. Checked on FF on Pixel and Chrome on generic droid. Thanks.




Consider applying for YC's Summer 2025 batch! Applications are open till May 13

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: