Last active
August 29, 2015 14:08
-
-
Save tobym/8592e5326a94ef53f1ce to your computer and use it in GitHub Desktop.
This script prints a random word to stdout.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
function print_random_word { | |
# The dictionary file. It contains one word per line. | |
dictionary=/usr/share/dict/words | |
# The number of words in the dictionary file. | |
num_words_in_dictionary=$(wc -l $dictionary | awk '{print $1}') | |
# A random number corresponding to a line in the dictionary file. | |
# This takes random data from /dev/random, converts it to an unsigned integer, and scales it by the number of words available. | |
random_line_number=$(($(cat /dev/random | od -N3 -An -D) % $num_words_in_dictionary)) | |
# Prints the word corresponding to the random line calculated above. | |
awk "NR == $random_line_number" $dictionary | |
} | |
print_random_word |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Along with mkString, this can implement the correct horse battery staple xkcd password generator: