Skip to content

Instantly share code, notes, and snippets.

@NonlinearFruit NonlinearFruit/README.md
Last active May 16, 2019

Embed
What would you like to do?
Simple random word generator

Ipsum

Ipsum is a simple command line random word generator with some handy features for esoteric users.

  • Off-line: uses dictionary, so no internet needed
  • Linux: it is a bash script, so it is Linux compatible
    • Note: may need to modify the location of the dictionary
  • Open Source: the code is public domain
  • Languages: can generate random words from non-English languages
  • Random: this uses /dev/urandom which apparently is a 'cryptographic PRNG'
  • Entropy: has option to display the entropy of the generated words

Usage

Here are some examples to illustrate how Ipsum works. It is also important to know that when translating, Ipsum displays the English word beneath the translated word and not all random words will translate. Once Ipsum generates a list of words (in whatever language you want), simply choose a subset to use as a password.

Default

$ ipsum
unbalanced

Specify Number

$ ipsum 12
tramp
cutoff
scoured
sundowns
indigence
pellagra
offs
heaping
peevishly
judiciary
unmerciful
haul

Alternative Language [German]

$ ipsum --lang de 5
enormousness
   ^ enormousness
Tonsuren
   ^ tonsures
Harfenist
   ^ harpist
Oligarchie
   ^ oligarchy
nachlassen
   ^ letup

Alternative Language [Japanese]

$ ipsum -l ja 5
再要求
   ^ reclaims
喫水線
   ^ waterline
同意しません
   ^ disagree
皮質
   ^ cortex
リクルーター
   ^ recruiter

Bits of Entropy

$ ipsum --entropy 4
4 word(s) from 1 language(s) produces 65.36 bits of entropy.

happening
commander
parred
woodland

Origins

The name, Ipsum, comes from Lorem Ipsum the name of filler text for graphical elements of a visual presentation.

License

This source code is provided under the Unlicense license. That is to say it is dedicated to the public domain.

#!/bin/bash
function ipsum
{
# Random Word Generator
# Inspired by:
# - https://linuxconfig.org/random-word-generator
# - http://stackoverflow.com/a/14203146/4769802
# - http://www.unix.com/shell-programming-and-scripting/156551-check-whether-string-begin-uppercase-lowercase-digit.html
#
# Depends on:
# - translate-shell: https://github.com/soimort/translate-shell
# - This ^ thing is awesome. Seriously, try it out!
#-----------------------------------------------------------------Constants
X=0
COUNT=1
MAX_INT=16777216
LANGUAGE=false
LANGUAGES="en"
NUM_LANGUAGES=1
ENTROPY=false
ALL_NON_RANDOM_WORDS=/home/bbolen/scripts/words.txt # dictionary file
non_random_words=`cat $ALL_NON_RANDOM_WORDS | wc -l` # total # of words
proper_nouns=`cat $ALL_NON_RANDOM_WORDS | grep [A-Z].* | wc -l` # apprx # of proper nouns
total_options=$((non_random_words - proper_nouns)) # apprx # of options
#-----------------------------------------------------------------Handle Flags w. Params
while [[ $# -gt 1 ]]
do
key="$1"
case $key in
-e|--entropy)
# print the entropy
ENTROPY=true
;;
-l|--lang)
# Get the specifed languages!
LANGUAGE=true
LANGUAGES=(${2//+/ })
NUM_LANGUAGES=${#LANGUAGES[@]}
shift # past argument
;;
--lang-all)
# Use top 10 languages at once!
LANGUAGE=true
LANGUAGES=(zh pt es bn en ru hi ja ar pa)
NUM_LANGUAGES=${#LANGUAGES[@]}
;;
*)
echo "Unrecognized argument: try 'ipsum --help'"
exit
;;
esac
shift # past argument or value
done
#-----------------------------------------------------------------Handle Main Param
if [[ -n $1 ]]; then
case $1 in
-a|--about)
# Give a description of self
echo "Ipsum is a minimalist random word generator"
echo "with several notable features:"
echo " - Off-line: uses local dictionary so"
echo " no internet is needed"
echo " - Languages: can generate non-English words"
echo " - Note: this requires the internet"
echo " - Random: uses '/dev/urandom' which is a CSPRNG"
echo " - Entropy: calculates entropy of requested word combo"
echo ""
exit
;;
-h|--help)
# Show some examples
echo ""
echo "Example Usage: ipsum"
echo " ipsum 12"
echo " ipsum -lang fr 12"
echo " ipsum -l fr 12"
echo " ipsum -l fr+ru 12"
echo " ipsum --entropy 12"
echo " ipsum -e 12"
echo " ipsum -l fr+ru -e 12"
echo ""
echo "Language Options:"
echo " - Found here :: https://github.com/soimort/translate-shell"
echo ""
exit
;;
-i|--info)
# Show information about numbers and such
echo ""
echo "Dictionary Location: $ALL_NON_RANDOM_WORDS"
echo "Dictionary Size: $non_random_words"
echo "Proper Nouns: $proper_nouns"
echo "Usable Words: $total_options"
echo "Default Language: $LANGUAGES"
echo "Possible Languages: 124"
echo " _________________________________ "
echo " | Top Ten Most Common Languages |"
echo " |----------------|----------------|"
echo " | Chinese - zh | Portuguese - pt|"
echo " | Spanish - es | Bengali - bn |"
echo " | English - en | Russian - ru |"
echo " | Hindi - hi | Japanese - ja |"
echo " | Arabic - ar | Punjabi - pa |"
echo " |________________|________________|"
echo ""
exit
;;
[0-9]*)
# It is probably a number, so work normally
COUNT="$1"
;;
*)
echo "Unrecognized argument: try 'ipsum --help'"
exit
;;
esac
fi
#-----------------------------------------------------------------Calculate Entrophy
if [ "$ENTROPY" = true ]; then
# Equation = log_2( #_of_options * #_of_languages ) * #_of_words
log_base_2=`echo "l($total_options*$NUM_LANGUAGES)/l(2)" | bc -l` # log base 2 of options
entropy_bits=`printf "%.*f\n" 2 $log_base_2` # bits of entropy per word
total_entropy=$(echo "$entropy_bits * $COUNT" | bc)
echo "$COUNT word(s) from $NUM_LANGUAGES language(s) produces $total_entropy bits of entropy."
echo ""
fi
#-----------------------------------------------------------------Make $COUNT Random Words
while [[ "$X" -lt "$COUNT" ]]; do
random_number=`echo "$(od -N3 -An -i /dev/urandom)*$non_random_words/$MAX_INT" | bc`
WORD=$(sed `echo $random_number`"q;d" $ALL_NON_RANDOM_WORDS)
# Clean and Format
DEL="'s"
WORD=${WORD%$DEL}
DEL="'"
WORD=${WORD%$DEL}
# Clean out 'funny' words (ie proper nouns)
case ${WORD:0:1} in
[[:lower:]])
# Lowercase indicates a common noun
if [ "$LANGUAGE" = true ]; then # If lang was specified use trans
random_index=`echo "$(od -N3 -An -i /dev/urandom)*$NUM_LANGUAGES/$MAX_INT" | bc`
LANG=${LANGUAGES[random_index]}
trans -b :$LANG $WORD 2> /dev/null
echo " ^ $LANG for $WORD"
else # else just given normal word
echo "$WORD"
fi
let "X = X + 1"
;;
*)
# Don't use whatever $WORD was
;;
esac
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.